Turn on thread page Beta
 You are Here: Home

# Writing a program to calculate prime numbers in Pascal watch

Announcements
1. Hi all,

As you can read from the title, I'm trying to write a program to calculate all prime numbers from 2 up to 1000, and then print them on separate lines with the writeln() statement. The language my college uses is Pascal (yes, I know it's old) and so far we've only advanced to for loops (yes, I know the algorithm will be inefficient). I can't figure out how to complete the program using only for loops, without any while or repeat loops. I assume next lesson we will go on to learn while loops, but for now I'm restricted to for loops. Thankfully, we have learned if / else, so they can be used too, and I assume they will be a big part of the program.

Any help would be awesome.
2. Personally I would use a Sieve of Eratosphenes. (This is presuming if you set an array of bools that the default value will be false)

Create an array of N bool.
Run a for loop that will run from i = 2 to sqrt(N).
if array[i] == false then:
Run a nested for loop that marks all the multiples of i that are smaller than N as true, but not i*1.
Also print out the value of i.

I hope this helps
3. I don't know pascal, but here's an ugly Java solution which you could adapt;

double count = 1;
This is the number you start with, count is the number that is checked to be a prime number or not. If you don't know what a double is, it may be called a float in your language; it's a number type that has a decimal place (count is actually "1.0", not just "1").
for (double n = 1, count<1000, n++) {
Declare another variable named "n" (or something more appropriate), we will use this number to check the count variable against and will increase this n number each time the for loop completes. The for loop will go on and loop around so long as count is less than 1000.
int result = count/n;
Speaks for itself.
if ( (count=n) && ((count/n) == (int)result) ){
if count is equal to n, AND the number from dividing count by n is an integer. (int)result gets you the integer value of the result, there's probably something in pascal like this.
print (count + " is an integer") to the console;
Whatever your print line code would be in pascal
n=0;
count++;}}
Resets the n in the for loop and adds 1 to the count variable (moving on to the next number to be checked)
else if (n>count){
count++;}
Moves onto the next number if n is greater than count, meaning that it was not a prime number. You can add another printline if you want to show the system identifying composite numbers.

Reply
Submit reply
Turn on thread page Beta
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 3, 2015
Today on TSR

### Four simple steps to get an A*

Here's all you have to do

### University open days

• University of East Anglia
All Departments Open 13:00-17:00. Find out more about our diverse range of subject areas and career progression in the Arts & Humanities, Social Sciences, Medicine & Health Sciences, and the Sciences. Postgraduate
Wed, 30 Jan '19
• Aston University
Postgraduate Open Day Postgraduate
Wed, 30 Jan '19
• Solent University
Careers in maritime Undergraduate
Sat, 2 Feb '19
Poll
Useful resources

## Make your revision easier

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.