Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Created January 20, 2013 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaProgMan/4578185 to your computer and use it in GitHub Desktop.
Save GaProgMan/4578185 to your computer and use it in GitHub Desktop.
One possible solution for problem 5 on the Project Euler website
/*
* Project Name: Problem Five
* Solution Name: Problem Five
* Original creation date: 09/07/2011
* Edit date: 18/01/2013
* Programmer name: Jamie Taylor (aka "GaProgMan")
* File name: ProblemFive.cpp
*
* Purpose of the project:
* This code is my solution for "Problem Five" listed on
* Project Euler.
* The original problem is listed in the comment block
* below.
* Initially, my idea was to brute force my way to the
* correct answer. The method for this is start with
* n=1, incrementing through a loop, and each time
* divide the number by all m numbers (1 to 20). The
* first number to pass each m number is the correct
* answer.
*
* Problem Five, from Project Euler.
* URL: http://projecteuler.net/index.php?section=problems&id=5
* 2520 is the smallest number that can be divided by
* each of the numbers from 1 to 10 without any remainder.
* What is the smallest positive number that is evenly
* divisible by all of the numbers from 1 to 20?
*
* GNU Copyright information
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <iostream>
using namespace std;
bool BruteForceCheck(long result);
bool SmarterCheck(long result);
//brute force checker
bool BruteForceCheck ( long result ) {
for (int m = 1; m <= 20; m++){
if (result % m != 0)
return false;
}
return true;
}
/*
* Smarter checker
* Algorithm/Ideas:
* We are told, in the problem statement, that: "2520
* is the smallest number that can be divided by each
* of the numbers from 1 to 10 without any remainder"
* Since we're looking at the numbers between 1 and
* 20, and we've been told the value for the numbers
* between 1 and 10, we can use that value and skip
* straight to checking 11 to 20.
* Test results:
* On my machine, the brute force check takes 10-13
* seconds to complete. By using a little savvy, and
* moving past the first 10 steps in the check (as
* we're provided with the answer for the first 10),
* this cuts execution time down to >0.5 seconds
*/
bool SmarterCheck (long result){
for (int m = 11; m <= 20; m++){
if (result % m != 0)
return false;
}
return true;
}
int main () {
long n = 1;
/*
* n will store the answer for this particular problem
* m will be each of the numbers (1 to 20) that n has
* to pass
*/
cout << "BRUTE FORCE CHECK" << endl;
//used for the bruteforce checker
for (n; !BruteForceCheck(n); n++);
cout << "The smallest number that can be evenly divided by 1 to 20 is:"
<< n << endl;
cout << "SMARTER CHECK" << endl;
n = 2520;
//used for the smarter checker
for (n; !SmarterCheck(n); n+=2520);
cout << "The smallest number that can be evenly divided by 1 to 20 is:"
<< n << endl;
char ch;
cin >> ch;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment