File: prime_numbers.tiny

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (26 lines) | stat: -rw-r--r-- 615 bytes parent folder | download
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
int is_factorable_by(int a, int b) {
    /* prime is not factorable by any number >= itself */
    if b >= a then return 0; end

    float quo := a / b;
    int iquo := quo;
    return quo = iquo;
}

int is_prime(int n) {
    if is_factorable_by(n, 2) then return 0; end
    if is_factorable_by(n, 3) then return 0; end
    if is_factorable_by(n, 5) then return 0; end
    if is_factorable_by(n, 7) then return 0; end

    /* n is prime, assuming it is not > 120 */
    return 1;
}

int main() {
    int i := 2;
    repeat
        if is_prime(i) then write i; write endl; end
        i := i + 1;
    until i > 120
}