| Project Euler, Project 2 |
[Jan. 12th, 2009|01:12 am] |
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. |
|
|