Prime numbers upto a given n
it is safe to assume that all n^2 and n^ +n, n^2 +n + n can be set to non prime. Take an example of 2 –> 4, 6, 8 all are multiples of 2 so not prime.
the for (int i = 2; i * i < n; i++) is because if we need to find primes till n, then we only need to care if there are 4, 8 etc in the order till n
public int countPrimes(int n) {
boolean[] isPrime = new boolean[n];
for (int i = 2; i < n; i++) {
isPrime[i] = true;
}
// Loop's ending condition is i * i < n instead of i < sqrt(n)
// to avoid repeatedly calling an expensive function sqrt().
for (int i = 2; i * i < n; i++) {
if (!isPrime[i]) continue;
for (int j = i * i; j < n; j += i) {
isPrime[j] = false;
}
}
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]) count++;
}
return count;
}How to determine a number is a fraction or not?
Modulo the number n % 1. If it is equal to 0 then it is a whole number, else its a fraction
How to find if a given number is divisible by a power of n?
Example check if x is a power of 3(n)
then 3^i = x then i=logx/log3
Some basic operations for conversions
2. String to int. Integer.parseInt(s)