Category Archives: Project Euler

Project Euler Problem #1 Solution in Java

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution in Java:

package problem1;
/**
* @author Khuram Ali
*/
public class Problem1
{

public static void main(String[] args)
{
int maxNum = 1000;
int result = 0;

for (int i = 2; i < maxNum; i++)
{
if (i % 5 == 0)
{
result += i;
}else if (i % 3 == 0)
{
result += i;
}
}
System.out.println (“total of multiples of 3 or 5: “) ;
System.out.println (result);
}
}

Project Euler Problem #4 C++ solution:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Solution: (Brute force)

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void check(int);

int main ()
{
int result = 0;

for (int i = 999; i >= 101; i–)
{
for (int j = i; j >= 101; j–)
{
result = (j *i);
check (result);
}
}
}

void check (int a)
{
string str = static_cast<std::ostringstream*>( &(std::ostringstream() << a) )->str();
string syl_1;
string syl_2;
string syl_3;
static string j ;
int len = (str.length()/2);
syl_1 = str.substr(0, len);
syl_2 = str.substr(len);

for (int i = syl_2.length(); i >=0; i –)
{
if (syl_2[i] != NULL)
syl_3 += syl_2[i];
}
if ((syl_1.compare(syl_3))==0)
{
if (j < str)
j = str;
if (str == j)
cout << j << endl;
}
}

Project Euler: Problem # 3 solution in C++

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ? */

#include <iostream>

using namespace std;

int main ()
{
long long factorOf  = (600851475143);
int num = 2;

while ((num * num) <= factorOf)

if (factorOf % num == 0)
{
cout << num << endl;
factorOf /= num;
}else
num++;

cout << “factor of 600851475143 is: ” << factorOf << endl;
}

Project Euler: Problem #2 solution in C++

/**(From:http://projecteuler.net/index.php?section=problems&id=2)

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Solution:*/

#include <iostream>

using namespace std;

int main()
{

int term_1 = 1;
int term_2 = 2;
int term_3 = 0;
int MaxTerm = 0;
int sum = 0;

cout<< “This Program lists the Fibonacci sequence. Please enter the Max number you want to have sequence:” << endl;

cin >> MaxTerm ;

while (term_1 <= MaxTerm)
{
cout<< term_1 << endl;
if ((term_1 %2) == 0){sum += term_1;}

term_3 = term_1 + term_2;
term_1 = term_2;
term_2 = term_3;

}
cout << “the sum of even value numbers is: “<< sum << endl;
}

Project Euler: Problem #1 solution in C++

/**(From:http://projecteuler.net/index.php?section=problems&id=1)

Problem Description:
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Solution:*/

#include <iostream>

using namespace std;

int main ()
{
int maxNum = 0;
int result = 0;

cout << “Please enter max number limit for the sum of all the multiples of 3 or 5: “;
cin >> maxNum;

for (int i = 2; i < maxNum; i++)
{
if (i % 5 == 0)
{
result += i;
}else if (i % 3 == 0)
{
result += i;
}
}
cout << “total of multiples of 3 or 5: ” << result << endl;

}