-1

The Practice of Computing Using Python 3rd edition by Punch and Enbody

I'm new to coding and it's been like 3 weeks. This is my assignment and I can't even get a glimpse of the answer. I reckon I have to get result when I put 6 for example like,

2 is prime.
3 is prime.
4 is not prime.
5 is prime.
6 is not prime.

All I know is real basic things like while, for, if, else, break and so on. And I have to solve this with only those.

And I cannot understand what that 'unnecessary checks' also means by. Please help.

This is my try and I know it's quite bismal.

N = int(input("Enter an int > 1:"))
k = 2

while k < N:
    for i in range(2, N):
        if k % i == 0:
            print(k, "isn't prime!")
            break
        else:
            k += 1
else:
    print(k, "is prime!")
001
  • 13,291
  • 5
  • 35
  • 66
  • In this context, the unnecessary checks would refer to "numbers greater than `sqrt(N)`". There are also other ways you can make it simpler, like when you are testing primality by hand, you would not check 4 after 2 is checked but these are a lot more harder to implement. You could skip all even numbers other than 2 but it's not generalized. – yxz Mar 29 '23 at 10:31
  • If you are in a bind, you could always check out some existing algorithms, understand them, and try implement them yourself. Check out: https://www.baeldung.com/cs/prime-number-algorithms. These are pseudocodes, but really its just a matter of syntax. – skyzip Mar 29 '23 at 10:36
  • it has already been answered [here](https://stackoverflow.com/questions/1801391/how-to-create-the-most-compact-mapping-n-%E2%86%92-isprimen-up-to-a-limit-n) – hidebyte Mar 29 '23 at 10:37
  • It would be better to use a `for` loop than `while` for `k`. (Your current code has a bug where you do `k += 1` as soon as you find a number that isn't a factor of `k`: if you used a `for` loop instead of `while`, you wouldn't need to do `k += 1` at all). – slothrop Mar 29 '23 at 10:37

3 Answers3

1

While there are definitely ways to optimize this code, reshuffling what you have now, adding an input check and changing the range of your loop (up to k instead of N) will make it work:

N = 0
while N < 2:
    N = int(input("Enter an int > 1:"))
k = 2

while k < N:
    for i in range(2, k):              # we should only check up to sqrt(k), but this still works. just inefficient.
        if k % i == 0:                 # if we check up to N, we always get i == k, and k % k == 0, so no number was prime.
            print(k, "isn't prime!")
            break
    else:                              # make sure to print if the for-loop exits without breaking, not after the while loop!
        print(k, "is prime!")
    k += 1                             # we always have to increment k after our prime checks are done!

My preferred method would be keeping a list of primes and marking multiples of primes as composite:

N = 0
while N < 2:
    N = int(input("Enter an int > 1:"))

is_prime = [False] + [True] * (N-1)  # boolean list that tells us which numbers are prime

for k in range(2, N):
    if is_prime[k]:                  # if the k-th value is True in our list
        for i in range(k, N, k):     # then for all multiples of k
            is_prime[i] = False      # we know that k is a divisor, so they are not prime!
        print(f"{k} is prime!")
    else:
        print(f"{k} isn't prime!")

This also stores the prime values in a nice, portable list that you can use again.

B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11
0

There is solution that uses recursion (its a more efficient way to run loops)

How do I find a prime number using recursion in Python

You need to read N as variable and pass it to is_prime which returns True or False

So example solution will be like this:

n = int(input("Enter n: ")
if(is_prime(n,2)):
  print("is prime)
else:
  print("not prime")
Max
  • 26
  • 3
-2

you can review this code

N = int(input("Enter an int > 1:"))

for num in range(2, N + 1):
    key = False
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
               break
        else:  
            key = True  

    if key:
        print(num, "is prime!")
    else:
        print(num, "isn't prime!")

Hapy coder ;)