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.

12.20.2009

The outcome has begun

A couple of years ago, I sat in a computer class with a few friends trying to understand - for the first time the concept of Markup. HTML was a killer then. I could not come to accept the fact that a simple tag would not be validated without the doctype. It was my first introduction to programming whatsoever. I thrived in the field for about 2 years - on and off because I had other courses in my university that took a great deal of my time.

But now, a musician came up. One with a profound sound that cannot be found elsewhere (no pun intended), The Violinist. I was blessed to be the designer of his website. I moved away from HTML and the lie in search for "greener" pastures and found this. I could not believe my head with the immense possibilities this was posing. After groping in the hardcore HTML class, I had finally created something off that but still in the same field.

All thanks to Ms Chow - who was my teacher for HTML. I could not have done any of these without frustrating you with my nibbling questions. Your persistence gave me the ground with which I pursued higher heights. And I hope you are a Mrs. by now.

4.06.2009

Ways To Exit A Page

Have you ever read a boring article and got really fed up you cannot find your mouse to close the page?
Have you ever been chatting with a friend through an instant messaging client and s/he starts spamming you sporadically with swear words? You just want to shut her/him up right?
Or for the sex deprived teenagers...& married couples, have you ever been watching pornography on your desktop computer and your father/mother/spouse abruptly walks in? BUSTED!

In this post, I talk about the different ways to exit a page.

First of all, you have to identify the operating system on your computer. Windows, Mac or Linux
Hint: If you do not know which operating system you are using, you are probably using Windows or some distribution of Linux unknown to mankind. If you do not know what an operating system is or someone had to help you to get to this site or s/he gave you a link and said, "Click it! That's all you have to do." You are definitely using Windows. If you had to comfigure, takedown, reprogram and reconfgure your machine to reach this site, you are probably on a Linux distribution. The particular one, you have to find out. If your mouse or trackpad (the mouse thingy that sits on your laptop) has only one button, most times, that's a Mac.

Now we have figured out what operating system we are using, skim through the post. Every operating system has a section which is highlighted.

Windows
1. Ctrl+W. Hold down Control and press W. This closes the window.
2. Alt+F4. Hold down Alt and press F4. This quits the application.
3. Move the mouse pointer along the tab. Find the 'X' and CLICK IT!
4. On the top right corner of the window, weighing 3.14 pounds, we have...the Red X. CLICK IT!
5. On the Menu bar, choose File->Quit (0r File->Close).
5. On the Taskbar, right-click on the window reference and choose close.

Linux
Now the techniques in this section apply to all distributions of linux.
1. Ctrl+W. Hold down Control and press W. This closes the window.
2. Ctrl+F4. Hold down Control and press F4. This quits the application.
3. Open the Terminal (Command Line Interface), type "killall {application name}". Where {application name} is to be replaced with the application name you want to quit.
4. On the Menu bar, choose File->Quit (or File->Close).
5. Ctrl+Alt+{Direction}. Hold the Control button, Alt and press any key on the direction pad. This does not close any page or application but it quickly switches desk-spaces.

Mac
"With one button, how can this machine perform many functions?"
1. Command+W. Hold Command and press W. This closes the window.
2. Command+Q. Hold Command and press Q. This quits the application.
3. Alt+Command+Esc. Hold alt (option) and Command and press Escape. This opens the Force Quit option. You then select the application you cant to quit.
4. Command+{Direction}. Hold the Command button and press any direction button on the direction pad. This does not close any page or application but it quickly switches desk-spaces using Éxpose.
5. On the top left corner, click the red circle.

Now for the ultimate technique for closing annoying pages.
  • locate your power button,
  • place your finger on it,
  • look away,
  • push down,
  • wait for about 5-6 seconds,
  • lift your finger,
  • look back at your computer,
  • remove your finger from the location of your power button.