Home
Andy's Blog - Project Euler, Project 2 [entries|archive|friends|userinfo]
Andy

[ website | Portal: Andy ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Project Euler, Project 2 [Jan. 12th, 2009|01:12 am]
Previous Entry Add to Memories Tell a Friend Next Entry
[Tags|]

http://projecteuler.net/index.php?section=problems&id=2

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>

using namespace std;

int numbers[1000];
int cnt = 0;

int IsMultiple(int nmbr, int mtpl)
{
        int yesno = 0;
        double multiplier = floor((double)nmbr / (double)mtpl);
        int temp = mtpl * (int)multiplier;

        if (temp == nmbr)
        {
                yesno = 1;
        }

        return yesno;
}

int Fibo(int top)
{
        int next = numbers[cnt-1] + numbers[cnt-2];

        if (next < top)
        {
                numbers[cnt] = next;
                cnt++;

                Fibo(top);
        }
}

int main(int argc, const char* argv[])
{
        int nlen;
        int top = 4000000;
        double total = 0;

        if (argc > 1)
        {
                top = atoi(argv[1]);
        }

        numbers[cnt] = 1;
        cnt++;
        numbers[cnt] = 2;
        cnt++;

        Fibo(top);

        nlen = sizeof(numbers) / sizeof(int);

        for (int x = 0; x < nlen; x++)
        {
                if (numbers[x] > 0 && IsMultiple(numbers[x], 2))
                {
                        total = total + (double)numbers[x];

                        cout << numbers[x] << endl;
                }
        }

        cout.precision(0);

        cout << "Total: " << fixed << total << endl;

        return 0;
}


UPDATED: I missed the part about even numbers initially thus the modified code.
LinkReply

Comments:
[User Picture]From: [info]thandrak
2009-01-12 07:54 pm (UTC)

(Link)

http://ces.gamespot.com/video.html?sid=6202973

Why I am not worried about Babs.
[User Picture]From: [info]jarodrussell
2009-01-12 08:27 pm (UTC)

(Link)

Hmm. Very interesting.