Just playing around with creating my own function to find all prime numbers between formal parameters 'start' and 'end'. Just curious if maybe there was a better way to incorporate the prime numbers less than 10 in the initial for statement as opposed to nesting those out separately. def prime(start,end): for x in range(start,end+1): if (float(x)/2).is_integer() or (float(x)/3).is_integer() or (float(x)/5).is_integer() or (float(x)/7).is_integer(): if x == 2 or x == 3 or x == 5 or x == 7: print x else: print x
Why would you use float in a prime test? Floats are inaccurate. Stick to integer math and it is more accurate... and easily does lower primes. http://dpaste.com/hold/1308510/
Yeah I remembered the % function later but I was just playing around with solving the problem another way.
In integers, you know how 1/3 is a representation of infinity because in decimal form it goes on forever? There are lots of things like that which crop up in converting between integers and floats. So looking for primes with floats means you have to constantly deal with that. Easier to just get the remainders. And for smaller primes, it can be easier still to just use a generated set of primes and see if things are in the set. For larger primes... well... there are lots of methods people use to test things. It is an area of research in computer science.
different "... way to incorporate the prime numbers less than 10 ..." - plus http://dpaste.com/1309259/ I didn't test that i like this algorithm- http://dpaste.com/1309247/
Yah, I was thinking about that. Drop out low primes early on. Then you can skip testing a ton of things. Even in a loop method like my quickly assembled one, testing %2 and then cycling through numbers from 3 on by odd numbers only would speed up the test a lot.
@Anarchtheist
seems like a cool program
Join our real-time social learning platform and learn together with your friends!