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

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

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

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;
}
LinkReply

Comments:
[User Picture]From: [info]frdelrosario
2009-01-13 11:16 am (UTC)

(Link)

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
[User Picture]From: [info]jarodrussell
2009-01-13 05:27 pm (UTC)

(Link)

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;
[User Picture]From: [info]frdelrosario
2009-01-13 06:12 pm (UTC)

(Link)

Oh! That's more like it! Is that Perl or Python?
[User Picture]From: [info]jarodrussell
2009-01-13 06:24 pm (UTC)

(Link)

Perl.