| Project Euler |
[Jan. 12th, 2009|12:16 am] |
In order to buff up my C++ skills, I'm going to start working through Project Euler. Here's my solution to Project 1.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
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 main(int argc, const char* argv[])
{
int total = 0;
int top = 1000;
if (argc > 1)
{
top = atoi(argv[1]);
}
for (int x = 1; x < top; x++)
{
int three = 0, five = 0;
three = IsMultiple(x, 3);
five = IsMultiple(x, 5);
if (three == 1 || five == 1)
{
total = total + x;
cout << x << " is a multiple of 3 or 5." << endl;
}
}
cout << "All those multiples below " << top << " equal: " << total << "." << endl;
return 0;
}
|
|
|
| Comments: |
Holy crap.
This is exactly why I'm neither a software professional nor a C++ guy.
I like Project Euler. So far I am 1-for-1. I will try to keep up with you.
theSum = 0
1.upto(999) {|i| if i%3==0 or i%5==0 then theSum = theSum+i end} print theSum
I think .upto may be one of my favorite aspects of Ruby. As for doing this in my preferred language:
my $sum;
for (1..999) { if ($_%3==0 || $_%5==0) { $sum += $_; }}
print $sum;
Oh! That's more like it! Is that Perl or Python? | |