# Creating a prime number generator by altering another code.

Page 1 of 1

## 4 Replies - 1457 Views - Last Post: 18 October 2012 - 10:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=296126&amp;s=ffcfa87e478003eef4e2ade3ebdd9a78&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chasebennett

Reputation: 0
• Posts: 11
• Joined: 12-October 12

# 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)

```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)

```

Is This A Good Question/Topic? 0

## Replies To: Creating a prime number generator by altering another code.

### #2 sepp2k

• D.I.C Lover

Reputation: 2616
• Posts: 4,173
• Joined: 21-June 11

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

### #3 chasebennett

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## 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 chasebennett

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## 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:

```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 sepp2k

• D.I.C Lover

Reputation: 2616
• Posts: 4,173
• Joined: 21-June 11

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