k = int (input ('Enter an integer...')) n = 0 while n<=k: t = 0 n = n + 1 for x in range(1,n + 1): if n%x == 0: t = t + x if x == n: if t == 2*n: print (n)
4 Replies - 1332 Views - Last Post: 18 October 2012 - 10:36 AM
#1
Creating a prime number generator by altering another code.
Posted 18 October 2012 - 04:24 AM
Hey, so I have this code that will generate all perfect numbers less than or equal to k. How would I alter this code to generate all prime numbers less than or equal to k? (using python 3)
Replies To: Creating a prime number generator by altering another code.
#2
Re: Creating a prime number generator by altering another code.
Posted 18 October 2012 - 04:32 AM
If it doesn't have to be efficient, you can write a function that checks whether a number is primer and then call that function for all numbers from 1 to k.
If it does need to be efficient, you should look into algorithms like the Sieve of Eratosthenes.
If it does need to be efficient, you should look into algorithms like the Sieve of Eratosthenes.
#3
Re: Creating a prime number generator by altering another code.
Posted 18 October 2012 - 04:38 AM
I'm not really looking for it to be efficient, but I'm wondering if I can tweak this code so that it only prints the numbers it finds to be prime, instead of perfect numbers.
#4
Re: Creating a prime number generator by altering another code.
Posted 18 October 2012 - 09:48 AM
Okay, so I'm starting to get it. This is what I have so far:
Everything seems to work just how I want, but the only problem is that I keep getting the next number above my set limit (k).
For example, if I input 50. I get every prime number up to 50, but I also get 53.
If I input 100, I get every prime number up to 100, but I also get 101.
Does anyone know how I can fix this?
k = int (input ('Enter an integer...')) n = 2 while n <= k: t = 2 while t < n: if n % t == 0: n = n + 1 t = 2 else: t = t + 1 print (n) n = n + 1
Everything seems to work just how I want, but the only problem is that I keep getting the next number above my set limit (k).
For example, if I input 50. I get every prime number up to 50, but I also get 53.
If I input 100, I get every prime number up to 100, but I also get 101.
Does anyone know how I can fix this?
#5
Re: Creating a prime number generator by altering another code.
Posted 18 October 2012 - 10:36 AM
The reason this happens is that the inner loop increments n as well, but doesn't check whether n stays smaller than or equal to k. So one fix would be to add the n <= k condition to the inner loop too.
However a better solution would be to move the prime checking code into its own function, which would simply return False when finding a divisor instead of continuing with the next n. That would definitely simplify the logic.
However a better solution would be to move the prime checking code into its own function, which would simply return False when finding a divisor instead of continuing with the next n. That would definitely simplify the logic.
Page 1 of 1