0

I'm trying to teach myself Pascal, and am putting together a program to determine prime numbers. It's crude, and inaccurate, but just a practice exercise. I've created a FOR loop that will see if a counted number has a remainder if divided by a set of prime numbers. If it doesn't it's not considered prime:

begin
  writeln('This program calculates all the integers below a given number');
  writeln('Please enter a number greater than 1');
  readln(number);
  //Need code to deal with entries that equal 1 or less, or aren't integers
  prime:=true;
  if number >=2 then writeln(2);
  if number >=3 then writeln(3);
  if number >=5 then writeln(5);
  if number >11 then writeln(7);

  For count := 1 to number do
      begin
      if count MOD 2 = 0 then prime:=false;
      if count MOD 3 = 0 then prime:=false;
      if count MOD 5 = 0 then prime:=false;
      if count MOD 7 = 0 then prime:=false;
      if prime = true then writeln(count);
      writeln ('count= ',count)
      end;

 writeln('Hit any key to continue');
 readln();

end. 

However, no matter what number I put in, the For loop prints 1 for the prime number. I've added a count print to see if the loop is working, and it seems to be. Any tips?

Thanks in advance!

TThoms
  • 1
  • 1
  • Please post the whole program. In your current code, the declaration of `number` is missing. – Roland Illig Mar 14 '19 at 06:34
  • 1
    You should step through your program using a debugger to see what it actually does. There's a great article called "how to debug small programs" that will help you find the bug. – Roland Illig Mar 14 '19 at 06:39
  • @Roland: that is [by Eric Lippert](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), and while it is interesting for someone not used to debugging, it doesn't really show how to do that in Pascal. Depending on the version of Pascal, that may be quite different. – Rudy Velthuis Mar 14 '19 at 08:13
  • A prime number is a whole number greater than 1 whose only factors are 1 and itself. So why are you testing whether 1 is a prime number? – Stuart Mar 15 '19 at 00:07
  • Thanks for all the comments! I appreciate the link to the discussion on debugging. Something I certainly need to learn. – TThoms Mar 15 '19 at 05:03
  • FWIW `'This program calculates all the integers below a given number'`. Did you mean **prime** integers? – Rudy Velthuis Mar 16 '19 at 13:39
  • FWIW, this loop should only print 1. For count = 2, prime is set to false and then it is never set to true anymore. – Rudy Velthuis Mar 16 '19 at 13:47

1 Answers1

2

Your variable prime is set to true before entering the loop. Inside the loop, when count is 1, the prime variable is not set again, hence it will print true.

In other words:

1 mod 2 equals 1
1 mod 3 equals 1
1 mod 5 equals 1
1 mod 7 equals 1

Since neither of these statements equals zero, the prime variable is not changed from its initial true value.


If you want to test if a number is a prime using a list of prime numbers, you should iterate from the list of prime numbers.

Here is a simple test that does that.

procedure TestIsPrime( number : Integer);
const
  // A loopup table with primes. Expand to cover a larger range.
  primes : array[1..4] of Integer = (2,3,5,7);
var
  count : Integer;
  highTest : Integer;
  IsPrime : Boolean;
begin
  if (number <= 0) then begin
    WriteLn('Illegal number: ',number);
    Exit;
  end;
  IsPrime := number > 1; // 1 is a special case !!
  if (number >= Sqr(primes[High(primes)])) then begin
    WriteLn('Needs more primes in table to test: ',number);
    Exit;
  end;
  highTest := Trunc(Sqrt(number)); // Highest number to test
  for count := 1 to High(primes) do begin
    if (highTest >= primes[count]) then begin
      if (number MOD primes[count] = 0) then begin
        IsPrime := false;
        Break;
      end;
    end
    else
      Break;
  end;
  if IsPrime = true then WriteLn(number);
end;
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Thanks for the lesson. In other words, because I have the boolean defined as true outside the loop, "1" gets through the loop as true because it always has a remainder. When "2" goes through, it obviously has no remainder when divided by 2, setting the boolean to "False", which it remains as "false" throughout the rest of the FOR count, because the "true" reset is outside of the FOR statement. Is that correct? Thank you for the code you offered, I'll add it to my programming study. (Arrays are the next chapter in the book!) – TThoms Mar 15 '19 at 05:17
  • Yes, stepping through the code line by line is a good lesson to learn. If you want to find all prime numbers up to a given number, start from 2, since 1 is per definition not a prime number. And do not test the count with a prime number equal or larger than the count. – LU RD Mar 15 '19 at 05:51