Basketball

My very first sports game! A simple basketball game. Get the ball into the basket. No nonsense, really. The physics for this game was relatively simple. And fun. But then again, physics is always fun.

The key here is to setup a good aiming system. The game builds itself around that. One of the best solutions is to represent exactly half the parabolic path of the ball. The player will still have to judge the path of the ball, but the portion that we show on the screen would give him an idea of the velocity and angle.

Our interest is in calculating the path of the ball, which is determined by two parameters:

  • The velocity
  • The launch angle

Given that the mouse pointer represents the highest point on the parabolic path, both launch velocity and launch angle can be calculated. It’s really simple (should you have taken physics in high school).

Parabolic path of the ball

 

In the above equations R is the range of the projectile and H is the maximum height to which it rises. It’s easy to see why, for a given position of the mouse, R and H are constants that can be calculated. Rearranging and solving these equations will give us the velocity and angle which is what we need to describe the motion. The velocity will then be resolved into components, for convenience.

The game depends on a few libraries like SOIL for texture loading, freeglut for openGL and irrKlang for audio.

Coming up next is a version with highscore tables! Gotta learn a little php for that 😛

Edit: High score table is live but a little buggy.

Available on gumroad:
Basketball

Source

Project Euler – Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Problem 10 is reasonably demanding. Unless you are familiar with Eratosthenes Sieve method (or any other sieve method), solving this problem in less than a minute will be next to impossible.

Sieve of Eratosthenes

In mathematics, the sieve of Eratosthenes , one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e. not prime) the multiples of each prime, starting with the multiples of 2.
Wikipedia Entry

General Algorithm

Above is a visual representation of the algorithm. Now, we must translate it into program code, in order to solve the problem.

  1. Declare an Boolean array with 2,000,000 elements (since we want to find primes less than 2 million).
  2. Fill the entire array with ‘true’ or 1. By doing this, we are assuming that all the numbers are prime. We will now “cross out” (mark false) those numbers which are multiples of other numbers.
  3. For each value of ifrom 2 to 2000000:
    1. Visit the ith element of the array. If it is true, then:
      1. For each element (j) which is a multiple of i, change the value of array[j]to false.
      2. Add the value of i to the sum.
    2. Else, continue iterating.
  4. Print the sum.

On my machine, the program takes about 0.063 seconds to run. It’s slightly memory intensive, requiring an array of 2000000 bytes to be allocated, but it can’t be helped.
If you know of some other prime number sieve which works faster, do let me know.

Solution in C++ 

Project Euler – Problem 8

Problem 8 of Project Euler gets a little tricky! In order to save yourself some time, you must know a little bit of file input and output in whatever language you program in.
While the number is small enough to be attacked by brute force, it can be a pain to hand-code it into an array. Copy-paste the number into a text file or download it from here.

 

The problem, as stated on the website:

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

General Algorithm

  1. To solve this problem, we read each digit from the file, and push it onto vector <int> number. This is done till we reach the end of the file.
  2.  Once we have the number in our vector/array, we iterate through it, 5 digits at a time, and find the product. At the end we display the maximum product.

Solution in C++

Note: Syntax for File I/O varies differs based on language.

Project Euler – Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Problem 7 is pretty easy too!

General Algorithm

  1. Begin testing numbers to see if they are prime, starting from 2.
  2. Break the loop when you have found 10001 prime numbers.

It’s that simple. 102,892 people have solved this problem.

Run time: 0.062 s.  Do let me know if you manage to make an improvement.

Solution in C++

Project Euler – Problem 3

Problem 3 of Project Euler is a computational beast. Nothing more. The problem, as stated on the website is:

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

What is the largest prime factor of the number 600851475143 ?

General Algorithm

Before we start, we have to take a few things into account:

  1. 600851475143 is an insanely large number. Languages like C/C++ have no native support for big integers. Luckily for us, we can store this in a long long unsigned int.
  2. While testing for primes, the following information must be kept in mind:
  3. While the definition of prime numbers is known to most, the second point is rather important. It implies that we need only to check for factors of a number till √n and not till n.

The algorithm is as follows:

  1. Initialise const long long unsigned int n = 600851475143.
  2.  Iterate i through every integer from √n to 1.
  3. If i perfectly divides n, then:
    • If i is prime then the highest factor is i
    • If i is not prime, then check if n/i is prime. If n/i  is prime, then n/i is the highest factor.
    • If neither i nor n/i  is prime, then continue through the loop
  1. Break the loop.

Solution in C++ 

Project Euler – Problem 2

The Problem:

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.

General Algorithm:

Again, a fairly iterative problem. It explains what the Fibonacci sequence is. In terms of mathematical operations, it is defined as:

 

The process is continuous. So continue to repeat this, to get successive terms.

  1. Initialize a sum variable to 0.
  2. Iterate through all the term of the Fibonacci sequence.
  3. If a term is more than 4,000,000 then break the loop
  4. Otherwise add it to the sum.
  5. When the loop breaks, print the sum.

Solution in C++