: :
A plus B... again
: : I’m not really clear about what you’re asking here. Surely you’re
: : aware that
: :
: :
: : Sum := A + B ;
: :
: :
: : will add A and B, and put the result in Sum.
: :
: : And there will be limits on A and B. The biggest integer type is
: :
LongInt which takes up four bytes. The greatest value that
: : can be stored in a LongInt is +2,147,483,647.
: :
: :
Primes
: : This one’s easier. A number is prime if it cannot be evenly divided
: : by any prime number that is less than it is. You need an array to
: : keep track of all the prime numbers your program has found thus far,
: : up to 100. Establish an index, i, and initialize it to 2. Then go
: : into a loop. In psuedocode:
: :
: :
: : j := 2 ;
: : i := 1 ;
: : while i <= N do
: : if (j is prime) then
: : A[i] := j
: : i := i + 1
: : end
: : j := j + 1
: : end
: :
: :
: : Then print out the array
: :
: :
: : for j := 1 to N do
: : writeln (A[j])
: :
: :
: : The big challenge here is to determine if “j is prime.” This is
: : best handled by a function
: :
: :
: : function isprime(j : LontInt) : Boolean ;
: :
: :
: : which returns TRUE is the number is prime. Giving IsPrime access to
: : the Array it can determine this by
: :
: :
: : j mod A[i] = 0
: :
: :
: : for all the A[i] found so far. IsPrime is FALSE if this statement
: : is ever TRUE.
: :
: :
: so what would the final "prime" question look like...the whole code?
: sry really new to this so got all confused what you said would be
: easier to see if the whole code was there at once. And for the "a
: plus b 2" thats all they have give but this doesnt work:
:
:
: readln(a,b);
: writeln(a+b);
:
:
: and they also gave a hint which i forgot to mention before is that
: it said it uses string and array.
:
Here's a program that prints out all primes up to 1000 using an algorithm called the Seive of Eratosthenes. I found it at
http://www.scriptol.org/sieve.php#pascal
I've reformatted it and made some minor changes.
Program Eratosthenes ;
{
a program to print out all primes up to 1000
using the Sieve of Eratosthenes.
}
CONST
N = 1000 ;
Var
P : Array [1 .. N] of Boolean ;
i,j,m : Word ;
begin
for i := 1 TO N do
P[i] := TRUE ;
m := trunc(sqrt(N)) ;
for i := 2 to m do
if P[i] then
for j := 2 to N DIV i do
P[i * j] := FALSE ;
for i := 1 to N do
if P[i] then
write(i:4) ;
end.
With a couple of modifications this can be the program you need. First, is 1 prime? According to this program it is, but from your example printout it is not. Whatever! You can include a statement to set P[1] := FALSE.
Second, you need to use a counter so that the program prints out only the number of primes you want, not all of them. And of course you need to include a prompt and a readln to get the number primes you want to print.