I am taking part in Project Euler, which is a set of mathematical problems, which require the development of algorithms and the writing of computer programs for their solutions.
The solution to each problem is a number which has to be typed in on the website. The website keeps track of all the problems that you have solved. Upon the submittal of a correct solution, you are granted access to a forum where you may discuss the method for solving the problem with other successful users.
I started last week Friday and have solved the first two problems so far out of the current 254 (new problems are added regularly).
I plan to use the Fortran programming language throughout to improve my skills in that langauge. The first two problems were very easy:
- Find the sum of all multiples of 3 or 5 less than 1000.
- Find the sum of all even-valued terms in the Fibonacci sequence which do not exceed four million.
I simply used Fortran to write simple programs to calculate the sums. The third problem is giving me a headache:
The reason I have a headache is that 600,851,475,143 is about 300 times larger than the largest integer that a 32-bit computer can hold, 2,147,483,647.
I had a simple plan for this problem:
- Translate my prime number generating code from Visual Basic to Fortran and store it in a program called Primes.f95. DONE!
- Write the prime numbers to a text file called Primes.txt. DONE!
- Write another program, PrimeFactor.f95, for calculating the prime factors of an integer fed in by the user.
- PrimeFactor.f95 would contain an array, Primes(Size), which would be filled with primes read in from the Primes.txt text file.
- The fed-in integer would then be divided by the primes in the Primes(Size) array to find its prime factors.
- The largest of the prime factors I would then type into Project Euler and, E Voila!, have three problems solved.
One way to get past this is to specify the Primes(Size) array as of the REAL data type, instead of as an integer, because 600,851,475,143 is miniscule in comparison with the largest real number that a 32-bit computer can hold, 100,000,000,000,000,000,000,000,000,000,000,000,000 or 1E+38 .