PDA

View Full Version : [WIP] Project Euler



legionaire45
October 3rd, 2008, 02:38 AM
I just sat down and finished Problem 1 (http://projecteuler.net/).
It took me 2 or so hours.

BUT I BEAT THIS DERP.

/*
Morgan Cabral - 10/02/08
Project Euler - Problem 1
Header File
*/

#ifndef PROBLEM_1_H
#define PROBLEM_1_H

// Function Blueprints
int findTotalMultiples ( int base, int threshold); // Finds the total of all of the multiples
// of the supplied number combined up until
// the supplied threshold value.

#endif // PROBLEM_1_H
/*
Morgan Cabral - 10/02/08
Project Euler - Problem 1
Source File
*/

#include <iostream> // Standard Library
#include "problem_1.h" // Custom Header w/ Function Blueprints

using namespace std;

// Main Function
int main()
{
// Constants
const int THRESHOLD_NUMBER = 1000; // The number that all multiples must be lower than.

// Variables
int finalTotal = 0; // The final total value.

// Math
finalTotal = findTotalMultiples ( 3, THRESHOLD_NUMBER ) // Add the total of all of the multiples of three
+ findTotalMultiples ( 5, THRESHOLD_NUMBER ); // to that of all of the multiples of five.
cout << "The total of all of the multiples of 3 and 5 is: " // Print the answer to the console.
<< finalTotal << endl;

int theTest = 0; // D E B U G
cin >> theTest;

return 0;
}

/*
Morgan Cabral - 10/02/08
Project Euler - Problem 1
Source File
*/
// Headers
#include <iostream>
#include "problem_1.h"
using namespace std;

/*********************************************
Function Declaration - int findTotalMultiples
This function calculates the sum of
all of the multiples of the number
given via "base" up until the
"threshold" number.
*********************************************/
int findTotalMultiples ( int base, int threshold )
{
int i = 0; // Counter.
int currentMultiple = 0; // The current multiple to add to the subtotal.
int subTotal = 0; // The combined value of all the currently
// calculated multiples.

while ( currentMultiple < threshold - base % threshold )
/* While currentMultiple is less then the threshold minus
the remainder of base modulo threshold... */
{
i++; // Iterate the counter...
currentMultiple = base * i; // Calculate the current multiple...
subTotal += currentMultiple; // Add that to the subtotal...

/* cout << base << " x " << i // D E B U G - print out info
<< " = " // Uncomment to get a printout
<< currentMultiple // In the console.
<< endl; */
}

return subTotal;
}

Plz comment on my shitty coding skills, lulz.

legionaire45
October 4th, 2008, 12:42 AM
Comment dammit! :F

Well, looks like I messed up the first time around - I fixed it up and now it passes their test. Woot.

/*
Morgan Cabral - 10/03/08
Project Euler - Problem 1.2
Header File
*/

#ifndef PROBLEM_1_2_H
#define PROBLEM_1_2_H

// Function Blueprints
/***********************************************
Function Blueprint - findSumMultiples
Takes in two variables and finds the multiples
of these variables up until a certain threshold.
If there are duplicate values then they are
only calculated once.
***********************************************/
int findSumMultiples ( int base1, int base2, int threshold );


#endif //PROBLEM_1_H
/*
Morgan Cabral - 10/02/08
Project Euler - Problem 1
Source File
*/

#include <iostream> // Standard Library
#include "problem_1_2.h" // Custom Header w/ Function Blueprint

using namespace std;

// Main Function
int main()
{
/*
// Modulo test
int test1 = 0;
int answer1 = 0;

for ( test1 = 1; test1 <= 20; test1++ )
{
answer1 = 5 % test1;
cout << "5 % " << test1 << " = " << answer1 << endl;

answer1 = test1 % 5;
cout << test1 << " % 5 = " << answer1 << endl;
}
*/


/*************************************
VARIABLES/CONSTANTS
*************************************/
const int THRESHOLD_NUMBER = 1000; // The number that all multiples must be lower than.

cout << findSumMultiples ( 3, 5, THRESHOLD_NUMBER ) << endl; // Print out the results of the function
// (See findSumMultiples.cpp for more info).

int theTest = 0; // D E B U G
cin >> theTest;

return 0;
}

/*
Morgan Cabral - 10/03/08
Project Euler - Problem 1.2
Source File
*/
// Headers
#include <iostream>
#include "problem_1_2.h"
using namespace std;

/*********************************************
Function Declaration - findSumMultiples
Takes in two variables and finds the multiples
of these variables up until a certain threshold.
If there are duplicate values then they are
only calculated once.
*********************************************/
int findSumMultiples ( int base1, int base2, int threshold )
{
/*************************************
VARIABLES
*************************************/
int multBase1 = 0; // Multiple of this particular base1 iteration.
int multBase2 = 0; // Multiple of this particular base2 iteration.
int i = 0; // Counter.
int sumMultBase1 = 0; // The sum of all of the multiples of base1.
int sumMultBase2 = 0; // The sum of all of the multiples of base2.

/************************************
FUNCTION
************************************/
while ( multBase1 < threshold - ( threshold % base1 ) ) // While the multiples of base1 are less than
{ // the threshold minus the remainder of threshold/base1.
i++; // Iterate the counter.
multBase1 = base1 * i; // Set the current multiple for this iteration.

if ( multBase1 % base1 == 0 && multBase1 % base2 == 0 ) // If the multiple is a multiple of both bases...
cout << multBase1 << " IS A MULTIPLE OF BOTH " // D E B U G
<< base1 << " AND " << base2 << endl; // D E B U G

else // If the multiple is not a multiple of both bases...
sumMultBase1 += multBase1; // Add the current multiplier to the total.

cout << base1 << " * " << i << " = " << multBase1 // D E B U G
<< ", "; // D E B U G
cout << "Total: " << sumMultBase1 << endl; // D E B U G
}

i = 0; // Set the counter back to 0.

while ( multBase2 < threshold - ( base2 % threshold )) // While the multiples of base2 are less than
{ // the threshold minus the remainder of threshold/base2.
i++; // Interate the counter.
multBase2 = base2 * i; // Set the current multiple for this iteration.

sumMultBase2 += multBase2; // Add the current multiplier to the total.

cout << base2 << " * " << i << " = " << multBase2 // D E B U G
<< ", "; // D E B U G
cout << "Total: " << sumMultBase2 << endl; // D E B U G
}

// cout << threshold % base2 << endl; // D E B U G

return ( sumMultBase1 + sumMultBase2 );
}...and the much simpler method of doing it here. (http://projecteuler.net/project/resources/001_c619a145e9d327a5c4c84649bec9981b/001_overview.pdf)
On to Number 2!

EDIT: Much simpler method of the same thing (pulled from that PDF) in C++:

#ifndef PROBLEM_1_S_H
#define PROBLEM_1_S_H

/********************************
Function Definitions
********************************/
int sumDivisibleBy( int n, int target = 999 );

#endif // PROBLEM_1_S_H
#include <iostream>
#include "problem_1_s.h"
using namespace std;

int main()
{
cout << sumDivisibleBy( 5 ) + sumDivisibleBy( 3 ) - sumDivisibleBy ( 15 )
<< endl;

int lolwut;
cin >> lolwut;
return 0;
}
#include <iostream>
#include "problem_1_s.h"
using namespace std;

int sumDivisibleBy( int n, int target )
{
int p;
p = target / n;

return n * ( p * ( p + 1 ) ) / 2;
}Lol, my version fails.

Advancebo
October 4th, 2008, 08:25 AM
is this a comment?

what is it?

SMASH
October 4th, 2008, 09:32 AM
Eh? C++ seems a bit too complex for such a simple program... I'll give a run at it in JAVA with some of my computer science buddies when I get back to school.

EDIT: Wow! Haha, am I dumb! I just remembered that this is pretty basic PreCalculus. The multiples of any number create a geometric sequence: A1 : 3, A2 : 3(2^1)=6, A3 : 3(2^2)=9, ect.

So the equation for finding the sum of all the multiples of 3 is: 3((1-2^1000)/(1-2))

... or at least it should be... my graphing calculator gives me an overflow error, so idk... but hopefully that put you on a better track?





EDIT 2: Sorry, didn't seem you completed it... >.<

legionaire45
October 4th, 2008, 11:26 AM
C/C++ is the only language that I "know" at the moment although I guess that adapting to something easier/C-based wouldn't be too hard.

I'm trying to get decent with C++ since our Robotics team needs programmers.