blob: 49c545aff6c4d793f0303031ca5a3732fa34f131 (
plain)
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
|
#include "user.h"
void _start(void)
{
put("primes up to N, enter N (empty line quits)\n");
char line[32];
for (;;)
{
put("> ");
int length = read_line(line, 32);
put("\n");
if (length == 0)
break;
long limit = 0;
for (int position = 0; position < length; position++)
if (line[position] >= '0' && line[position] <= '9')
limit = limit * 10 + (line[position] - '0');
int count = 0;
for (long candidate = 2; candidate <= limit; candidate++)
{
int prime = 1;
for (long divisor = 2; divisor * divisor <= candidate; divisor++)
if (candidate % divisor == 0)
{
prime = 0;
break;
}
if (prime)
{
put_int(candidate);
put(" ");
count++;
}
}
put("\n");
put_int(count);
put(" primes\n");
}
sys_exit(0);
}
|