about summary refs log tree commit diff stats
path: root/tests32/test05.c
blob: c6a4e67f41bb0f497a9f5ac81cfa0eeace919685 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int fact(int i) {
    if (i<2) return i;
    return i*fact(i-1);
}

#define SET(M) dels[M/8] |= (1<<(M%8))
#define GET(M) ((dels[M/8]>>(M%8))&1)

int main(int argc, const char** argv)
{
    int j = 5;
    if(argc>1)
        j = atoi(argv[1]);
    if(j==0)
        j=5;
    if(j>15) j=15;
    
    int k = fact(j);
    printf("fact(%d)=%d\n", j, k);

    uint8_t* dels = (char*)calloc((k+7)/8, 1);
    SET(0);
    SET(1);
 
    for (int i=2; i<k; i++)
        if (!GET(i)) {
            int m = 2 * i;
            while (m < k) {
                SET(m);
                m += i;
            }
        }
 
    printf("Prime list 0..%d: ", k);
    for (int i=0; i<k; i++)
        if (!GET(i))
            printf("%d ", i);
    printf("\n");

    free(dels);

    signed char sc = -5;
    unsigned char uc = 83;
    signed short int ss = -53;
    unsigned short int us = 65500;

    int it = sc+uc+ss+us;

    printf("(un)signed char = %hhd/%hhu (un)signed int = %hd/%hu total=%d\n", sc, uc, ss, us, it);

    printf("%hu/5=%hu, %hu%%5=%hu\n", us, us/5, us, us%5);
    printf("%d/5=%d, %d%%5=%d\n", it, it/5, it, it%5);

    printf("%d/%hd=%d + %d", it, ss, it/ss, it%ss);

    return 0;
}