hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/user/primes.c
blob: 5501c329601cdfd9bc8c1070c4f9faa67a2cb627 (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
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("primes up to N, enter N (empty line quits)\n");
    char line[32];
    for (;;)
    {
        printf("> ");
        int length = readline(line, 32);
        printf("\n");
        if (length == 0)
            break;

        long limit = atol(line);
        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)
            {
                printf("%ld ", candidate);
                count++;
            }
        }
        printf("\n%d primes\n", count);
    }
    return 0;
}