I was making a program which could tell if a given number is a prime number or not. No matter whether I enter a prime number or another number, it always shows "it is not a prime number". Is there any fault in it?
10 input "what is the number";a
20 let b=1
30 let b=b+1
40 let k=a/b
50 let p=fix(k)
60 if p=k then goto 100
70 if b<a then goto 30
80 print "it is a prime number"
90 goto 110
100 print "it is not a prime number"
110 end
run
Follow the logic:
a.bas1.1tob, so thatbis now2.ktoa/b. This means thatkis now half ofa.ptokwithout the .5 that may or may not be there.p(half ofarounded down) is equal tok(half ofanot rounded down), that is, ifais divisible byb, it goes to 100 and saysit is not a prime number.b(which is2) is less thanathe program goes to line 30 and adds another1toband repeats the process, but nowbis3.b(which is2) is equal toaor greater than it, it prints that this is a prime number.Let's say that the number you enter is 2. Two is, in fact, a prime number. Follow the logic above, however, and you will see that the program will tell us that it is not a prime number, because at step 6, two divided by two is one, and one truncated is still one. The same should be true for any number except 1, because all numbers are divisible by themselves.
My guess is that in your testing, you never tested 1; the program should say that 1 is a prime number (this is because even though 1 is divisible by itself, you start at b=2).
(Note also that this is also technically wrong: one is not a prime number.)