Tag Archive for c++

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

									

TJU Problem 1551 Solution

Yea… I got bored tonight, can’t sleep… solved another problem.

 

Digital Roots is a little bit tricky… if you know Machine Language (Assembly) its really simple since you’re thinking registers and ascii… if you don’t know Assembly, I dropped a hint for you… try to figure it out before looking at the solution ;)

 

C++ Solution: Simple solution using a few basic C concepts… utilizing the C-style string format, input is a long string that gets checked at each character. the sum is measured through ascii values of the characters… and so forth, I’m sure you got it by now.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s;
    cin >> s;
    while (s != "0"){
        int sum = 0;
        for(int i = 0; i < s.length() ; i++)
            sum += (s[i] - 48);
        if(sum / 10 == 0)
            cout << sum << "n";
        else{
            int root = sum;
            while(root / 10 !=0){
                sum = 0;
                while (root != 0){
                    sum += root % 10;
                    root /= 10;
                }
                root = sum;
            }
            cout << sum << "n";
        }
        cin >> s;
    }

    return 0;
}
									

**For some odd reason the code box is eliminating small things like back slashes (\). So the little typos aren’t really my fault… this question has been submitted and accepted by the TJU website. **

 

 

TJU Problem 3013 Solution

Greetings,

First problem I solve on TJU… figured I’d start posting these up here…

Alfredo’s Pizza Restaurant isn’t such a complex question, very straight forward math… There aren’t any secrets to the code really, fairly simple question to solve.

 

C++ Solution: This solution is linear, brute force solution.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int main()
{
   int r, w, l, count=1;
   double pizzaRadius=0.0;

   while(true)
   {
      scanf("%d",&r);
      if(r==0) break;
      scanf("%d%d",&w,&l);
      pizzaRadius = sqrt( (w*1.0)*(w*1.0) + (l*1.0)*(l*1.0) ) / 2.0;
      if( pizzaRadius <= r*1.0 )
         printf("Pizza %d fits on the table.n",count);
      else
         printf("Pizza %d does not fit on the table.n",count);
      count++;
   }

   return 0;
}
									

**For some odd reason the code box is eliminating small things like back slashes (\). So the little typos aren’t really my fault… this question has been submitted and accepted by the TJU website. **

 

Java Solution: It has a small bug… not in the Algorithm, but somewhere small… I wanna show an algorithm, not give code for free solutions :P

import java.util.Scanner;
import java.lang.Math;

public class TJU3013 {

  public static void main (String[] args){
    Scanner input = new Scanner(System.in);
    int count = 1;
    int r, l, w;
    double PizzaRadius = 0.0;
    while(true){
      r = input.nextInt();
      if(r == 0)
        break;
      l = input.nextInt();
      w = input.nextInt();
      PizzaRadius = Math.sqrt((w*1.0)*(w*1.0) + (l*1.0)*(l*1.0)) / 2.0;
      if(PizzaRadius <= r*1.0)
        System.out.printf("Pizza %d fits on table.n", count);
      else
        System.out.printf("Pizza %d does not fit on the table.n", count);
      count++;
    }
      
  }
}