Rewrote my totient calculator in C. It’s pretty much a direct translation of the excrement I wrote in Rust. Be gentle, there are obvious points that I miss completely in C that veterans in the ancient language would scoff heartily about. I don’t yet know what the hell a struct is for or the extents of threading. All in good time.

So here’s my uncommented and fully unnecessary data type usage.

Behold:

 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
52
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

unsigned long int gcd(unsigned long int e, unsigned long int f);
unsigned long int tot(unsigned long int t);

int main(int argc, char *argv[])
{
    if (argc < 3 || argc > 3) { printf("Usage: %s first second\n",
argv[0]); exit(0); }

    unsigned long int a = strtol(argv[1], NULL, 10);
    unsigned long int b = strtol(argv[2], NULL, 10);
    unsigned long int i;

    if (a > b) { printf("First must be lower than second.\n"); }

    for ( i = a ; i <= b ; i++ )
    {
        printf("%d, %d\n", i, tot(i));
    }

    exit(0);
}

unsigned long int gcd(unsigned long int a, unsigned long int b)
{
    unsigned long int c;
    while ( a != 0 ) 
    {
        c = a;
        a = b % a;
        b = c;
    }
    return b;
}

unsigned long int tot(unsigned long int t)
{
    unsigned long int y = 0;
    unsigned long int o = t;
    while ( t != 0 ) 
    {
        if (gcd(o, t) == 1) 
        {
            y++;
        }
        t--;
    }
    return y;
}