Archive for Project Euler

Project Euler Problem 6 Solution – Sum Square Difference

Sum Square Difference also known as problem 6 is very straight forward math… (A) calculate the summation of the numbers squared, and (B)then the square of the summation… then subtract A from B… my solution for this problem is below in C++

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

using namespace std;


int main(){
    int sumfirsthundred = 0;
    int squarefirsthundred = 0;
    int sumfirsthundredsqr = 0;
    for(int i = 1; i < 101; i++){
        sumfirsthundred = sumfirsthundred + i;
        squarefirsthundred = squarefirsthundred + (i*i);
    }
    sumfirsthundredsqr = sumfirsthundred * sumfirsthundred;
    cout << sumfirsthundredsqr - squarefirsthundred << "n";
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
    return 0;
}
									

 

Project Euler Problem 7 Solution – 10001st Prime

10001st Prime is another prime calculating problem… nothing hard. my solution is below and written in C++.

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

using namespace std;

int isPrime(int number){
int count=0;
for(int a = 1; a<=number ;a++)
{
if(number % a == 0)
count++;
}
if(count == 2)
return number;
return 0;
}

int main(){
int primecount = 0;
int num = 1;
int prime = 0;
bool working = true;
while(primecount != 10001){
prime = isPrime(num);
num++;
if(prime > 0){
primecount++;
}
}

cout << prime <<"n";
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
return 0;
}

 
									

Project Euler Problem 14 Solution – Longest Collatz sequence

Longest Collatz sequence is a very cool problem which I first encountered (requiring different things, but method was the same) when I started learning C++. The problem states that for any set of positive integers you do the following:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

and that eventually, any number will end up at 1. the problem requires us to count how many numbers are in the chain between n and 1… the solution to this classic project Euler problem is below in Java.

 

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */

public class p14 {

    public static void main(String[] args){
        int longestChain = 0;
        int finalNumber = 0;
        int count = 1;
        double currentNumber = 0;
        for(int i = 2; i < 1000000; i++){
            currentNumber = i;
            while(currentNumber != 1){
                if(currentNumber % 2 == 0)
                    currentNumber = currentNumber/2;
                else
                    currentNumber = 3*currentNumber + 1;
                count++;
            }
            if(count > longestChain){
                longestChain = count;
                finalNumber = i;
            }
            count = 0;
            System.out.println(i);
        }
        System.out.println(finalNumber);
    }
}

									

Project Euler Problem 4 Solution – Largest Palindrome Product

Largest Palindrome Product is a pretty cool problem (problem 4) that requires you to find the largest palindrome made of a product of two 3-digit numbers. a palindrome is a number that if inverted, it has the same value (ex. 9009, 1221, 69496, etc.). Unlike other programming websites, Project Euler doesn’t care too much about performance of your code, elegance, etc. but rather wants a final answer. Since I recently stumbled on some old code I wrote last year, I figured I’d share it… the solution to problem 4 is below and written in C++. it might take a moment or two to spit the final answer, but after all Project Euler is only interested in final answer.

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

using namespace std;


int main(){
    string tempreverse;
    string temp;
    stringstream out;
    int tempnumber, tempnumber2; 
    int palindrome = 0;

    for(int i = 100; i < 1000; i++){
        for(int j = 100; j < 1000; j++){
            out.str("");
            tempnumber = i * j;
            out << tempnumber;
            temp = out.str();
            tempreverse = string (temp.rbegin(), temp.rend());
            tempnumber2 = atoi(tempreverse.c_str());
            if (tempnumber == tempnumber2){
                if(tempnumber > palindrome){
                    palindrome = tempnumber;
                }
            }
        }
    }
    std::cout << palindrome << "n";
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
    return 0;
}