6.21.2010

Problem 7 - Nth Prime [Project Euler Problems]

This problem requires you to give the 10001th prime number so I named it the Nth prime problem. It's pretty simple when you get to understand primes and appreciate them for their beauty. If you are a beginner in programming and haven't taken a course that covers runtimes, you may find this problem really annoying as you may spend much money on coffee while trying to stay awake watching and waiting for when the results will pop out on your screen. I finished this in less than 20 minutes (15 of those minutes were spent trying to figure out the truth behind these numbers).

The code I wrote is more interactive this time as it allows you specify the number of primes you want. Be careful; though this is relatively fast, if you input a really large number, you may have to leave your computer running for a couple hours.
The code is as below:
#include <iostream>
#include <list>
using namespace std;

int main (int argc, char * const argv[]) {
 
 // Number of primes the user will specify
 int numberOfPrimes;

 // STL container that dynamically stores all the prime numbers.
 list primes;

 // An iterator for loopint through this container.
 list::iterator primeIterator; 
 
 // This is the little trick. I know the first prime number is 2,
 // so that is injected into the list
 primes.push_back(2);
 
 // Request for the number of primes.
 cout << "Enter number of primes you want: ";
 cin >> numberOfPrimes;
 
 /* RUNTIME GUARD */
// if (numberOfPrimes > 999) {
//  cout << "Number is too large" << endl;
//  return 0;
// }
 
 /* Computation Loop 
  * This is where all compoputation is done.
  *
  * To do the wor are a couple new actors. 
  * Introducing the cast, we have: 
  *     i -> keeps count of the number of primes and makes
  * sure the user gets his required number.
  *     j -> presents numbers to be processed for 'prime'ness
  *     isPrime -> gets high when a 'j' is prime 
  * (This does not refer to hitting Js and getting high.
  * I meant it in the most programming terms possible).
  */
 for (int i = 0, j = 2; i < numberOfPrimes - 1; ++j) {
  bool isPrime = true;
  for (primeIterator = primes.begin();
    primeIterator != primes.end(); ++primeIterator) {
   if (j % (*primeIterator) == 0)
   isPrime = false;
  }
  if (isPrime) {
   // Add the number to the primes collection
   primes.push_back(j);
   ++i;
  }
 }
 
 // Print out the Nth prime number.
 primeIterator = primes.end();
 --primeIterator;
 cout << *primeIterator << endl;
 
 return 0;
}

The result is 104743.

Problem 6 - Project Euler Problems - Squares Sum Squared

After some disappointment with Problem 5, I got to wet my appetite with some basic computation. Problem 6 requires you to find the difference between the sum of the numbers between a range and the sun of the suares of the same numbers between the same range.

The code I used is as below:
#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {
  cout << "PROJECT EULER PROBLEM 6\n" << endl;
 
  int start(1), end(100), sum(0), squareSum(0), sumSquare(0);

  for (int i = start; i <= end; ++i) {
    int square = i * i;
    squareSum += square;
    sum += i;
  }
 
  sumSquare = sum * sum;
 
  cout << sumSquare - squareSum << endl;
  return 0;
}
The result is 25164150.

Problem 5 - Project Euler Problems - LCM

After solving the fourth problem, I was pumped to do some more coding on the fifth one and tae on even harder coding challenges as the list number increased. To my dismay, this was a big L.C.M (Lowest Common Multiple) problem. A 6th grader can solve this. The result is 232792560.

Anyways, you can view my solution.

Problem 4 - Project Euler Problems - Palindromes

This problem requires you to find the largest product of three digit numbers that is a palindrome.

There are 2470 of such palindromes; the highest being 993 * 913 [906609] and the lowest being 101 * 101 [10201]

The code I used to get this is as below. Feel free to loo through it, modify and comment! You can view the output file here.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main (int argc, char * const argv[]) {

  int num1;
  int num2;
  string resultString;
 
  for (num1 = 999; num1 > 99; --num1) {
    for (num2 = 999; num2 > 99; --num2) {
      int result = num1 * num2;
      stringstream resultStream;
      resultStream << result;
      resultString = resultStream.str();
   
      {
        /* CHECK FOR PALINDROME */
        int stringSize = resultString.size();
        int midpoint = stringSize / 2;
        bool fault = false;
    
        for (int i = 0; i <= midpoint; ++i) {
          if (resultString[i] != resultString[stringSize - i - 1]) {
            fault = true;
            break;
          }
        }
    
        if (!fault) {
          cout << "Palindrome found!   "
               << num1 << " * " << num2
               << " = " << resultString 
               << " which is a palidrome." 
               << endl;
        }
      }
    }
  }
 
  cout << "Done" << endl;
 
  return 0;
}

Rejuvenation

Wazzappenin' people.

It's been a while since I blogged here. Other blogs have been rolling though but this one in particular has suffered. This was my first blog ever; coined by my first teacher in anything computer related. Allow me dedicate the next paragraph to Ms. Chow.

Ms. Chow. I hope you are a Mrs. by now. Thanks for taking me through that tedious HTML class in which I never listened to you. You refused to give up even when I did & now I'm going somewhere ith my web programming skills and programming in general. Thank you.

Now, I have gone far with my computer pursuits lately and I owe it to this blog to further its content. Recently, boredom made me discover the Project Euler problems. I will be posting my solutions here. Most of these problems (if not all of them) require you to code. I will be posting my source code and its output here.

Through this, I plan to rejuvenate this blog. Hope you enjoy and get inspired too.