Python Forum
nested for loop dilemma 2 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: nested for loop dilemma 2 (/thread-21066.html)

Pages: 1 2


nested for loop dilemma 2 - YoungGrassHopper - Sep-12-2019

Hey guys.

So what I have today is another nested loop issue.
Program needs to ask a user for a number and save as variable (num)
I then need to determine if the number is bigger than 1, and if so:
determine if the number given by the user is a prime number or not.

Chances are fair that my approach is wrong but here is what I have come up with:
Trying to use a while loop as outer loop to start the inner for loop calculation
ONLY IF the number given by the user is greater than one.

at first I only had the for loop but I get a false positive if the user enters the number 1.

It seems to work apart from the fact that its now an infinite loop, and moving the break command back seems
to kill the calculation.

Any advice would be very much appreciated.
Here is what I have:

num = int(input("Enter a number here: "))

while num >= 2:
    for i in range(2, num):
        if num % i  == 0:
            print(num," is not a prime number.")
            break
    else:
        print(num," is  a prime number.")



RE: nested for loop dilemma 2 - luoheng - Sep-12-2019

Replace while with if:
num = int(input("Enter a number here: "))
 
if num >= 2:
    for i in range(2, num):
        if num % i  == 0:
            print(num," is not a prime number.")
            break
    else:
        print(num," is  a prime number.")



RE: nested for loop dilemma 2 - perfringo - Sep-12-2019

Maybe some.... analysing before coding:

- 2 is one and only even prime
- there is no need to check even numbers beyond that - they are all not primes
- there is no need to try to divide with even numbers - odd numbers are not divisible with even numbers

Just using these observations you can significantly reduce brute-force calculations. And of course, there is math about primes....


RE: nested for loop dilemma 2 - YoungGrassHopper - Sep-12-2019

(Sep-12-2019, 09:18 AM)luoheng Wrote: Replace while with if:
num = int(input("Enter a number here: "))
 
if num >= 2:
    for i in range(2, num):
        if num % i  == 0:
            print(num," is not a prime number.")
            break
    else:
        print(num," is  a prime number.")

Thanks luoheng,

I first had it similar to this, if statement prior to the for loop but I must have had something wrong as it did not work quite right. That's when I tried the while outer loop instead. Anyways,
Thanks for the pointer appreciate it.


RE: nested for loop dilemma 2 - YoungGrassHopper - Sep-12-2019

(Sep-12-2019, 09:29 AM)perfringo Wrote: Maybe some.... analysing before coding:

- 2 is one and only even prime
- there is no need to check even numbers beyond that - they are all not primes
- there is no need to try to divide with even numbers - odd numbers are not divisible with even numbers

Just using these observations you can significantly reduce brute-force calculations. And of course, there is math about primes....

Sure, I should implement that routine before stating to code, 1st analyse and then approach for most efficient solution.
Are you suggesting that the for loop is a rather inefficient way to determine prime or not prime?
The reason why I went for the loop is because the lesson is about nested loops and I figured that is what they want me to use in order to solve the problem.

So would you say this is a more efficient way of getting to the answer or am I totally misunderstanding?

ps: I checked the lesson again now, and even though it is about nested loops in this specific task they do not specify that I NEED to make use of loops, so I think I am gong to opt for the code below instead.

num = int(input("Please enter a number here: "))
if (num%2 != 0)and(num >1)or(num == 2):
    print(num, " is a prime number.")
else:
    print(num, " is not a prime number.")



RE: nested for loop dilemma 2 - ichabod801 - Sep-12-2019

Okay, but your code says that 9 is prime.


RE: nested for loop dilemma 2 - YoungGrassHopper - Sep-12-2019

(Sep-12-2019, 11:40 AM)ichabod801 Wrote: Okay, but your code says that 9 is prime.

dammit, let me check
thanks for bringing it to my attention I just checked the first couple digits below 9


RE: nested for loop dilemma 2 - perfringo - Sep-12-2019

My thinking more along those lines:

- if num == 1 or if num % 2 == 0 -> it's not prime
- if num == 2 it's prime
- for other cases use loop to divide num with values in range(3, num, 2) to find whether there is divisor

It's still brute force, but 'half brute-force'.

EDIT:

The order is wrong. First should be num == 2 check.


RE: nested for loop dilemma 2 - YoungGrassHopper - Sep-12-2019

num = int(input("Please enter a number here: "))
if (num%2 != 0)and(num >1)and(num != 9)or(num == 2):
    print(num, " is a prime number.")
else:
    print(num, " is not a prime number.")
I hope this is bug free ha hah ah I see I have many struggles ahead in this new endeavour


RE: nested for loop dilemma 2 - perfringo - Sep-12-2019

I am afraid that you are very far from determining primes.

Maybe something along those lines:

def prime_checker(integer): 
    if integer == 2:                        # eliminate 2
        return True 
    if integer == 1 or integer % 2 == 0:    # eliminate 1 and all even integers
        return False 
    for i in range(3, integer, 2):          # divide only with odd numbers as integer can't be even
        if integer % i == 0: 
            return False 
    else: 
        return True