a combination of two similar yet different madness…

9
Prime Numbers & Hamming numbers A combination of two similar yet different madness…

Upload: clement-todd

Post on 16-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A combination of two similar yet different madness…

Prime Numbers & Hamming numbers

A combination of two similar yet different madness…

Page 2: A combination of two similar yet different madness…

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.

int getPrimeFactor

Page 3: A combination of two similar yet different madness…

int getPrimeFactor

Page 4: A combination of two similar yet different madness…

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 “.

Page 5: A combination of two similar yet different madness…

void printPrimeFactors

Page 6: A combination of two similar yet different madness…

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

int isHammingNumber

Page 7: A combination of two similar yet different madness…

int isHammingNumber

Page 8: A combination of two similar yet different madness…

Input and Output

Page 9: A combination of two similar yet different madness…

Thanks