Skip to content

Instantly share code, notes, and snippets.

@kylelong
Created September 28, 2019 22:22
Show Gist options
  • Save kylelong/62ac7236f294bd0e0f4a4085df5c844d to your computer and use it in GitHub Desktop.
Save kylelong/62ac7236f294bd0e0f4a4085df5c844d to your computer and use it in GitHub Desktop.
See growth rates of n for a few functions
public class Growth{
/**
n!,2n,2​n^2, 5nlogn,20n,10n
*/
public static void main(String [] args){
long fact, twon, twon2, fivenlogn, twentyn, tenn;
for(long i = 0; i < 20; i++){
fact = fact(i);
twon = (long)Math.pow(2, i);
twon2 = 2 * (long)Math.pow(i, 2);
fivenlogn = (5 * i) * (long)Math.log(i);
twentyn = 20 * i;
tenn = 10 * i;
System.out.printf("%d: n!: %d 2^n: %d 2n^2: %d 5nlogn: %d 20n: %d 10n: %d\n",
i, fact, twon, twon2, fivenlogn, twentyn, tenn);
}
}
public static long fact(long n){
if(n <= 2){
return n;
}
return fact(n - 1) * n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment