Bottom Page

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.")

Quote
#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.")
YoungGrassHopper likes this post
Quote
#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.
Quote
#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.
Quote
#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.")

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

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested Loop for user input Ads 2 135 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 322 Oct-16-2019, 03:11 PM
Last Post: stullis
  nested for loop dilemma YoungGrassHopper 9 395 Sep-13-2019, 03:56 AM
Last Post: jsira2003
  Nested while loop problem + turtle DreamingInsanity 3 413 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Index error using pop in nested loop PerksPlus 3 516 Mar-28-2019, 03:11 PM
Last Post: ichabod801
  Nested for loop strange problem mcva 2 439 Mar-16-2019, 12:53 PM
Last Post: mcva
  Nested while loop in pyramid program. raj55 2 2,058 May-04-2018, 10:05 AM
Last Post: ThiefOfTime
  Nested Loop Problem GSWes 5 1,385 Nov-12-2017, 10:22 PM
Last Post: Larz60+
  Using nested for loop with a single list mikeavison 3 1,069 Aug-12-2017, 08:13 PM
Last Post: ichabod801
  A "Nested Loop" example from python 3.3.4 to 2.6!! sarada2099 6 2,525 Dec-18-2016, 02:56 PM
Last Post: sarada2099

Forum Jump:


Users browsing this thread: 1 Guest(s)