Nov-21-2023, 07:41 PM
This is not printing what I expect so hopefully someone can point out what I'm not understanding.
Ultimately, the program gets this right:
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!
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: continueThe 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!