summaryrefslogtreecommitdiff
path: root/p9/main.c
blob: c75fea90842d97cd6f9c6b7a093ff4fc4f4d00ef (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
#include <stdlib.h>
#include <stdio.h>

int gen(int p, int pmax, int q)
{
    int a, b, c;

    for (; p < pmax; p += 2)
    {
        int _q = q;

        for (; _q < p; _q += 2)
        {
            if (!pythagorean_triplet(p, _q, &a, &b, &c))
                continue;

            printf("p:%d q:%d a:%d\tb:%d\tc:%d\n", p, _q, a, b, c);

            if (a+b+c == 1000)
                goto found;

            if (1000 % (a+b+c) == 0)
            {
                int m = 1000 / (a+b+c);

                printf("triplet sum (%d) is divisor of 1000, multiply by %d\n",
                    a+b+c, m);

                a *= m;
                b *= m;
                c *= m;

                if (a+b+c == 1000)
                    goto found;
            }
        }
    }

    return 0;

found:

    printf("got it: a:%d b:%d c:%d a*b*c=%d\n", a, b, c, a*b*c);

    return 1;
}

int main(int argc, char **argv)
{
    // some different ways to generate pythagorean triples, let's hope one of
    // these yields the result

    if (gen(3, 20, 3))
        return 0;

    printf("--\n");

    if (gen(3, 20, 2))
        return 0;

    printf("--\n");

    if (gen(2, 20, 3))
        return 0;

    // and of course the brute-force way :-) (this could be optimized by
    // making the condition b<1000 -> b<1000-a and just stating c=1000-a-b
    // instead of iterating values of c).

    // int a, b, c;

    // for (a = 1; a < 1000; a++)
    // {
    //     for (b = a+1; b < 1000; b++)
    //     {
    //         for (c = b+1; c < 1000; c++)
    //         {
    //             if ((a + b + c == 1000) && (a*a + b*b == c*c))
    //             {
    //                 printf("a:%d b:%d c:%d a*b*c:%d\n", a, b, c, a*b*c);
    //                 return 0;
    //             }
    //         }
    //     }
    // }

    return 0;
}