0
Help please
Hello World Can you help me by writing an algorithm to decide whether a number X (X<22) is a prime or not ? Using pseudocode preferable . Thank you so much 🌷🌷🌷
7 Antworten
+ 6
We can if you show us your attempt.
+ 4
In order to check if X < 22 you have to read it first. After reading X, check if it's two and if it is print "Prime". Also check if it's less than 2 and print "Not prime".
To check for division, use a for loop like this:
1. I <- 3, X/2 (because you already checked for lower values)
2. if (X%I==0) print "Not prime" and break the loop
3. If I reaches X/2 print "Prime"
I hope it's clear enough 🙂
+ 2
Well, you can, if you only want the primes up to 22'
+ 1
Good answer!
0
1. Start
2. If (X<22) then go to 3
3. Read X
4. B<- X-1
5. If (X/B) is even or gives no remainder then print " Not prime "
6. If (X/B) gives a remainder then go to 7
7. If (B=2) and (X/B) gives a remainder then print " prime "
8. End
This is what I have so far
0
What if I wrote this way ?! Is it right ?
1. Start
2. Read X
3. If (X<22) then go to 4
4. If (X=2) or (X=3) or (X=5) or (X=7) or (X=11) or (X=13) or (X=17) or (X=19) then print "prime" else print "not prime"
5. End
0
What if I wrote this way ?! Is it right ?
1. Start
2. Read X
3. If (X<22) then go to 4
4. If (X=2) or (X=3) or (X=5) or (X=7) or (X=11) or (X=13) or (X=17) or (X=19) then print "prime" else print "not prime"
5. End