-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes.p8
51 lines (49 loc) · 1.28 KB
/
primes.p8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// primes.p8
//
// Original Author: Dan Jurca
// Date Created: 1999 November 23
//
// Update Authors: Christopher Arnoult
// Nicholas Rebhun
// Last Modified: 2015 June 10
//
// Does this:
// prompts user for int nwp; reads int;
// if int < 1
// halts
// else
// prints first nwp primes.
// Notice that this program uses all four of the boolean relations on integers; it
// has nested whiles, a break, an if, and an if-else.
//
{
int d,n,np,nwp,p; // nwp is number of wanted primes
input nwp;
if ( nwp < 1) {
return;
}
output 2;
if ( nwp == 1 ) {
return;
}
n = 3;
output n;
np = 2;
while ( np < nwp ) {
n = n+2;
d = 3; // 1st trial divisor
p = 1; // flag- 1: yes, prime; 0, no
while ( d*d <= n) {
if ( d*(n/d) != n ) {
d = d+2; // next trial divisor
} else {
p = 0; // n is not a prime
break;
}
}
if ( p == 1 ) { // was n prime?
output n;
np = np+1;
}
}
}