I like to do small programming exercises to keep my skills sharp. Unfortunately I sometimes get a little carried away with them.
Problem: generate a list of primes numbers.
Sounds simple right? Well lets start to look at this. First lets define what prime numbers are. A prime number is a number which is divisible by only itself and one. A simple way to test if a number if prime is by trial division. To test to see if a number is prime you check whether it is division by any prime less then the square root of that number.
The general algorithm for finding the list of primes by trial division is therefore the following.
list of primes = {2, 3}
for each number begining at 4
check to see if it is divisible by any prime less then the square root of number
if number is prime then append it to list of primes
Well go through a sample implementation then see what we can do to improve it.
Problem: generate a list of primes numbers.
Sounds simple right? Well lets start to look at this. First lets define what prime numbers are. A prime number is a number which is divisible by only itself and one. A simple way to test if a number if prime is by trial division. To test to see if a number is prime you check whether it is division by any prime less then the square root of that number.
The general algorithm for finding the list of primes by trial division is therefore the following.
list of primes = {2, 3}
for each number begining at 4
check to see if it is divisible by any prime less then the square root of number
if number is prime then append it to list of primes
Well go through a sample implementation then see what we can do to improve it.
Comments
Post a Comment