90 likes | 260 Views
Prime Numbers & Hamming numbers. A combination of two similar yet different madness…. int getPrimeFactor. Basic concepts If a number is even, apart from 2 it is not a prime If a number is odd, its factors can be 3, 5, 7, 9, … We can essentially eliminate all the even factors
E N D
Prime Numbers & Hamming numbers A combination of two similar yet different madness…
intgetPrimeFactor • Basic concepts • If a number is even, apart from 2 it is not a prime • If a number is odd, its factors can be 3, 5, 7, 9, … We can essentially eliminate all the even factors • *** (Extra info.) The number is a prime if all factors up till sqrt (number) is not divisible by that number • E.g. number is 61, if 2, 3, 4, 5, 6, 7, 8 cannot divide the number, then there is no way another factor can occur in the range 9 to 61.
void printPrimeFactors • Basic concepts: • A number still has factors if it is not 1 • For the current number, print out its smallest factor • Then divide the number by its factor • Repeat the process until the number is 1 • *** (Optional) To print out the “x”, check if the number is not 1 (means still have factors) and print out “ x “.
intisHammingNumber • Basic concepts: • Divide the number by 2 repeatedly until it has no more factors of 2 • Repeat the process for 3 and 5 • If the resultant number is 1, means the number has no other factors and is a Hamming Number