vraag 1) hoe bedoel je dat er bij een prime geen break wordt aangeroepen, dit staat geïmplementeerd in de code die ik als voorbeeld had gegeven?
Update: oh zo.. inderdaad, in dit geval niet, omdat het dus anders is aangegeven.
vraag 2) en wat nu voor me in een keer weer onduidelijker is geworden, hoe de for loop bij iteratie zes na de 2e keer eruit komt? dus bij 1e keer is de mod 1, daardoor komt die er niet uit en wordt j opgehoogd. Bij de 2e keer is j 3, dus 7 / 3 = 2 en mod 1, maar de mod voldoet niet aan de controle van de if-statement en alsnog komt die na iteratie 6 eruit en print die 7 is a prime (natuurlijk is 7 een priemgetal, maar hoe kan het in dit geval omdat de logica van de code wat anders zegt..): en hetzelfde gebeurt ook bij iteratie 10, hoe kan het dat die eruit gaat?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| for-loop 1, iteration: 1 [i = 2, j = 2] and [cond.: 2 / 2 = 1] and [mod: 2 % 2 = 0]
2 is prime
for-loop 1, iteration: 2 [i = 3, j = 2] and [cond.: 3 / 2 = 1] and [mod: 3 % 2 = 1]
3 is prime
for-loop 1, iteration: 3 [i = 4, j = 2] and [cond.: 4 / 2 = 2] and [mod: 4 % 2 = 0]
4 is not a prime
for-loop 2, iteration: 4 [i = 5, j = 2] and [cond.: 5 / 2 = 2] and [mod: 5 % 2 = 1]
for-loop 1, iteration: 4 [i = 5, j = 3] and [cond.: 5 / 3 = 1] and [mod: 5 % 3 = 2]
5 is prime
for-loop 1, iteration: 5 [i = 6, j = 2] and [cond.: 6 / 2 = 3] and [mod: 6 % 2 = 0]
6 is not a prime
for-loop 2, iteration: 6 [i = 7, j = 2] and [cond.: 7 / 2 = 3] and [mod: 7 % 2 = 1]
for-loop 1, iteration: 6 [i = 7, j = 3] and [cond.: 7 / 3 = 2] and [mod: 7 % 3 = 1]
7 is prime
for-loop 1, iteration: 7 [i = 8, j = 2] and [cond.: 8 / 2 = 4] and [mod: 8 % 2 = 0]
8 is not a prime
for-loop 2, iteration: 8 [i = 9, j = 2] and [cond.: 9 / 2 = 4] and [mod: 9 % 2 = 1]
for-loop 1, iteration: 8 [i = 9, j = 3] and [cond.: 9 / 3 = 3] and [mod: 9 % 3 = 0]
9 is not a prime
for-loop 1, iteration: 9 [i = 10, j = 2] and [cond.: 10 / 2 = 5] and [mod: 10 % 2 = 0]
10 is not a prime
for-loop 2, iteration: 10 [i = 11, j = 2] and [cond.: 11 / 2 = 5] and [mod: 11 % 2 = 1]
for-loop 2, iteration: 10 [i = 11, j = 3] and [cond.: 11 / 3 = 3] and [mod: 11 % 3 = 2]
for-loop 1, iteration: 10 [i = 11, j = 4] and [cond.: 11 / 4 = 2] and [mod: 11 % 4 = 3]
11 is prime |
Nog een extra iteratie output waarbij ik zowel vóór de if-statement uitprint, als erná: misschien is dit wat duidelijker
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| for-loop 1, iteration: 1 [i = 2, j = 2] and [cond.: 2 / 2 = 1] and [mod: 2 % 2 = 0]
2 is prime
for-loop 1, iteration: 2 [i = 3, j = 2] and [cond.: 3 / 2 = 1] and [mod: 3 % 2 = 1]
3 is prime
for-loop 2 - after if, iteration: 3 [i = 4, j = 2] and [cond.: 4 / 2 = 2] and [mod: 4 % 2 = 0]
for-loop 1, iteration: 3 [i = 4, j = 2] and [cond.: 4 / 2 = 2] and [mod: 4 % 2 = 0]
4 is not a prime
for-loop 2 - after if, iteration: 4 [i = 5, j = 2] and [cond.: 5 / 2 = 2] and [mod: 5 % 2 = 1]
for-loop 2 - stuck..., iteration: 4 [i = 5, j = 2] and [cond.: 5 / 2 = 2] and [mod: 5 % 2 = 1]
for-loop 1, iteration: 4 [i = 5, j = 3] and [cond.: 5 / 3 = 1] and [mod: 5 % 3 = 2]
5 is prime
for-loop 2 - after if, iteration: 5 [i = 6, j = 2] and [cond.: 6 / 2 = 3] and [mod: 6 % 2 = 0]
for-loop 1, iteration: 5 [i = 6, j = 2] and [cond.: 6 / 2 = 3] and [mod: 6 % 2 = 0]
6 is not a prime
for-loop 2 - after if, iteration: 6 [i = 7, j = 2] and [cond.: 7 / 2 = 3] and [mod: 7 % 2 = 1]
for-loop 2 - stuck..., iteration: 6 [i = 7, j = 2] and [cond.: 7 / 2 = 3] and [mod: 7 % 2 = 1]
for-loop 1, iteration: 6 [i = 7, j = 3] and [cond.: 7 / 3 = 2] and [mod: 7 % 3 = 1]
7 is prime
for-loop 2 - after if, iteration: 7 [i = 8, j = 2] and [cond.: 8 / 2 = 4] and [mod: 8 % 2 = 0]
for-loop 1, iteration: 7 [i = 8, j = 2] and [cond.: 8 / 2 = 4] and [mod: 8 % 2 = 0]
8 is not a prime
for-loop 2 - after if, iteration: 8 [i = 9, j = 2] and [cond.: 9 / 2 = 4] and [mod: 9 % 2 = 1]
for-loop 2 - stuck..., iteration: 8 [i = 9, j = 2] and [cond.: 9 / 2 = 4] and [mod: 9 % 2 = 1]
for-loop 2 - after if, iteration: 8 [i = 9, j = 3] and [cond.: 9 / 3 = 3] and [mod: 9 % 3 = 0]
for-loop 1, iteration: 8 [i = 9, j = 3] and [cond.: 9 / 3 = 3] and [mod: 9 % 3 = 0]
9 is not a prime
for-loop 2 - after if, iteration: 9 [i = 10, j = 2] and [cond.: 10 / 2 = 5] and [mod: 10 % 2 = 0]
for-loop 1, iteration: 9 [i = 10, j = 2] and [cond.: 10 / 2 = 5] and [mod: 10 % 2 = 0]
10 is not a prime
for-loop 2 - after if, iteration: 10 [i = 11, j = 2] and [cond.: 11 / 2 = 5] and [mod: 11 % 2 = 1]
for-loop 2 - stuck..., iteration: 10 [i = 11, j = 2] and [cond.: 11 / 2 = 5] and [mod: 11 % 2 = 1]
for-loop 2 - after if, iteration: 10 [i = 11, j = 3] and [cond.: 11 / 3 = 3] and [mod: 11 % 3 = 2]
for-loop 2 - stuck..., iteration: 10 [i = 11, j = 3] and [cond.: 11 / 3 = 3] and [mod: 11 % 3 = 2]
for-loop 1, iteration: 10 [i = 11, j = 4] and [cond.: 11 / 4 = 2] and [mod: 11 % 4 = 3]
11 is prime |
Update 1: Komt het misschien omdat ondanks dat de binnenste for loop wordt gecheckt, de buitenste alsnog doorgaat zonder te kijken naar de conditie van de modulo operator?
Update 2: ik zie nu door een andere output te gebruiken dat die niet eens in de for loop 2 in gaat bij iteratie 6 en 10?? weet u hoe dat kan.. overal waar false bij staat wilt zeggen dat die erin is geweest, daarom kon ik nu zien dat het programma niet er ingaat, terwijl er wel restanten zijn en de conditie goed is denk ik?:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| for-loop 1, iteration: 4 [i = 5, j = 3] and [cond.: 5 / 3 = 1] and [mod: 5 % 3 = 2]
5 is prime
false
for-loop 1, iteration: 5 [i = 6, j = 2] and [cond.: 6 / 2 = 3] and [mod: 6 % 2 = 0]
6 is not a prime
for-loop 1, iteration: 6 [i = 7, j = 3] and [cond.: 7 / 3 = 2] and [mod: 7 % 3 = 1]
7 is prime
false
for-loop 1, iteration: 7 [i = 8, j = 2] and [cond.: 8 / 2 = 4] and [mod: 8 % 2 = 0]
8 is not a prime
false
for-loop 1, iteration: 8 [i = 9, j = 3] and [cond.: 9 / 3 = 3] and [mod: 9 % 3 = 0]
9 is not a prime
false
for-loop 1, iteration: 9 [i = 10, j = 2] and [cond.: 10 / 2 = 5] and [mod: 10 % 2 = 0]
10 is not a prime
for-loop 1, iteration: 10 [i = 11, j = 4] and [cond.: 11 / 4 = 2] and [mod: 11 % 4 = 3]
11 is prime |
[
Voor 48% gewijzigd door
umask op 16-11-2019 00:49
]