summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 8261fa0eee1191c36fc484be43ef3b262e3b209b (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const std = @import("std");
const print = std.debug.warn;

fn p1() void {
    var i: usize = 0;
    var sum: usize = 0;

    while (i < 1000) : (i += 1) {
        if (i % 3 == 0) {
            sum += i;
        } else if (i % 5 == 0) {
            sum += i;
        }
    }

    std.debug.warn("{}\n", .{sum});
}

fn p2() void {
    const size: usize = 35;
    var array: [size]usize = undefined;
    for (array) |_, i| {
        array[i] = 0;
    }
    array[0] = 1;
    array[1] = 2;

    var i: usize = 2;
    while (i < size) : (i += 1) {
        array[i] = array[i - 1] + array[i - 2];
    }

    i = 0;
    var sum: usize = 0;
    while (array[i] < 4_000_000) : (i += 1) {
        if (array[i] % 2 == 0)
            sum += array[i];
    }

    std.debug.warn("{}\n", .{sum});
}

fn is_prime(k: usize) bool {
    var i: usize = 2;
    var divided: bool = false;
    while (i < k) : (i += 1) {
        if (k % i == 0) {
            divided = true;
            break;
        }
    }
    return !divided;
}

fn p3() void {
    const size: usize = 5000;
    var primes: [size]usize = undefined;

    var i: usize = 1;
    var j: usize = 0;
    while (j < size) : (i += 1) {
        if (is_prime(i)) {
            primes[j] = i;
            j += 1;
        }
    }

    const n: usize = 600851475143;
    //const n: usize = 13195;
    var biggest: usize = 0;
    for (primes) |prime| {
        if (n % prime == 0) {
            biggest = prime;
        }
    }

    std.debug.warn("{}\n", .{biggest});
}

fn p4() !void {
    var i: usize = 0;
    var j: usize = 0;
    var str: [6]u8 = undefined;
    var largest: usize = 0;
    while (i < 1000) : (i += 1) {
        j = 0;
        while (j < 1000) : (j += 1) {
            _ = try std.fmt.bufPrint(&str, "{}", .{i * j});
            if (i * j > largest and
                str[0] == str[5] and
                str[1] == str[4] and
                str[2] == str[3])
            {
                largest = i * j;
            }
        }
    }
    print("{}\n", .{largest});
}

fn p5() void {
    var num: usize = 20;
    var i: usize = 1;
    var j: usize = 0;
    var found: bool = false;

    while (i < 1_500_000_000) : (i += 1) {
        j = num;

        found = false;
        while (j != 0) : (j -= 1) {
            if (i % j != 0) {
                found = true;
            }
        }

        if (!found) {
            print("{}\n", .{i});
            break;
        }
    }
}

pub fn main() !void {
    //p1();
    //p2();
    //p3();
    //_ = try p4();
    p5();
}