Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime number detector
#1
This is not printing what I expect so hopefully someone can point out what I'm not understanding.

for i in range(2,101): #prime number detector
    for j in range(2, 1 + int(round(i/2, 0))):
        print(f'(i , j) is ({i},{j})') 
        #print('i is ', i)
        #print('j is ', j)
        if i % j == 0:
            print(i/j)
            decision = 'composite'
            continue
        else: continue
The idea is to have the program test numbers 2-100 inclusive. Into each number, I want to divide 2 through the first integer greater than 50% of the number in question (over half and we know the result is going to be between 1-2, which amounts to a nonzero modulus). What the program is looking for is a zero modulus because that means another integer factor exists besides the number itself and 1.

Ultimately, the program gets this right:

Output:
Prime numbers under 100 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].
However, I don't understand why some loop pairings don't get printed. The first 32 lines of output are as follows:

Output:
(i , j) is (3,2) (i , j) is (4,2) 2.0 (i , j) is (5,2) (i , j) is (6,2) 3.0 (i , j) is (6,3) 2.0 (i , j) is (7,2) (i , j) is (7,3) (i , j) is (7,4) (i , j) is (8,2) 4.0 (i , j) is (8,3) (i , j) is (8,4) 2.0 (i , j) is (9,2) (i , j) is (9,3) 3.0 (i , j) is (9,4) (i , j) is (10,2) 5.0 (i , j) is (10,3) (i , j) is (10,4) (i , j) is (10,5) 2.0 (i , j) is (11,2) (i , j) is (11,3) (i , j) is (11,4) (i , j) is (11,5) (i , j) is (11,6) (i , j) is (12,2)
I would expect this to begin with i = 2. Then, L2 becomes j in range (2, 1 + int(rnd(1,0)) or (2,2) so it will do nothing since the start is actually beyond the exclusive ending point. This is consistent with the output.

Next, i = 3. L2 is then range(2, 1 + int(rnd(1.5, 0))) or range(2, 3). This should proceed only for j = 2. That is consistent with the output.

Next, i = 4. L2 is then range(2, 1 + int(rnd(2, 0))) or range(2, 3). This should proceed only for j = 2. That is consistent with the output.

Next, i = 5. L2 is then range(2, 1 + int(rnd(2.5, 0))) or range(2, 4). This should proceed for j = 2 and j = 3. Why does only (5,2) print out for i = 5?

Next, i = 6. L2 is then range(2, 1 + int(rnd(3, 0))) or range(2, 4). This should proceed for j = 2 and j = 3. That is consistent with output.

Next, i = 7. L2 is then range(2, 1 + int(rnd(3.5, 0))) or range(2, 5). This should proceed for j = 2, j = 3, and j = 4. That is consistent with output.

Next, i = 8. L2 is then range(2, 1 + int(rnd(4, 0))) or range(2, 5). This should proceed for j = 2, j = 3, and j = 4. That is consistent with output.

Next, i = 9. L2 is then range(2, 1 + int(rnd(4.5, 0))) or range(2, 6). This should proceed for j = 2, 3, 4, and 5. Where is (9, 5)?

i = 10, 11, and 12 are consistent with output.

Next, i = 13 (not shown). L2 is then range(2, 1 + int(rnd(6.5, 0))) or range(2, 8). This proceeds for j = 2, 3, 4, 5, and 6 but (13, 7) never prints. Why?

Thanks!
Reply


Messages In This Thread
Prime number detector - by Mark17 - Nov-21-2023, 07:41 PM
RE: Prime number detector - by deanhystad - Nov-21-2023, 08:19 PM
RE: Prime number detector - by Mark17 - Nov-22-2023, 06:28 PM
RE: Prime number detector - by jefsummers - Nov-21-2023, 08:39 PM
RE: Prime number detector - by jefsummers - Nov-21-2023, 08:39 PM
RE: Prime number detector - by deanhystad - Nov-27-2023, 12:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pairs of multiplied prime number--->N Frankduc 13 3,724 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  Face detector project? korenron 2 2,170 Mar-24-2021, 03:43 PM
Last Post: korenron
  qrcode detector not in cv2 Pedroski55 2 5,221 Sep-16-2020, 03:22 AM
Last Post: Pedroski55
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,991 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  check if the number is a prime integer atlass218 5 3,034 Sep-26-2019, 07:58 AM
Last Post: atlass218
  Smoke detector + send email Brandon99 4 4,032 Sep-12-2018, 11:18 PM
Last Post: Brandon99
  Creating a program to look for the largest prime number of a number Wikki14 4 3,969 Sep-08-2018, 12:30 AM
Last Post: Skaperen
  Unexpected result in simple prime number example jackhj 2 3,067 Apr-20-2018, 01:48 AM
Last Post: jackhj
  python prime number algorithm zowhair 3 3,860 Dec-20-2017, 06:22 PM
Last Post: zowhair
  Vowels and Consonants detector OmarSinno 5 9,856 Sep-21-2017, 02:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

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