4 Replies - 839 Views - Last Post: 18 October 2012 - 10:36 AM Rate Topic: -----

#1 chasebennett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 chasebennett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 chasebennett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1