Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loop dilemma 2
#1
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.")
Reply
#2
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.")
Reply
#3
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....
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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.
Reply
#5
(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.")
Reply
#6
Okay, but your code says that 9 is prime.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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
Reply
#8
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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
Reply
#10
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,331 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,443 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,178 Nov-25-2020, 10:04 AM
Last Post: palladium
  dilemma with list comprehension spalisetty06 1 1,896 Aug-14-2020, 09:18 AM
Last Post: buran
  Nested loop indexing Morte 4 3,809 Aug-04-2020, 07:24 AM
Last Post: Morte
  Nested for loop not looping puttingwordstogether 0 1,672 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,834 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Nested Loop for user input Ads 2 3,525 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 5,611 Oct-16-2019, 03:11 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020