Category Archives: Programming

Java solution to Project Euler Problem 5

Problem 5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Running time: 125ms

Assessment: LOL.

public class Problem005
{
	public static void main(String[] args)
	{
		long begin = System.currentTimeMillis();
		int i = 20;
		while (true)
		{
			if (	(i % 1 == 0) &&
				(i % 2 == 0) &&
				(i % 3 == 0) &&
				(i % 4 == 0) &&
				(i % 5 == 0) &&
				(i % 6 == 0) &&
				(i % 7 == 0) &&
				(i % 8 == 0) &&
				(i % 9 == 0) &&
				(i % 10 == 0) &&
				(i % 11 == 0) &&
				(i % 12 == 0) &&
				(i % 13 == 0) &&
				(i % 14 == 0) &&
				(i % 15 == 0) &&
				(i % 16 == 0) &&
				(i % 17 == 0) &&
				(i % 18 == 0) &&
				(i % 19 == 0) &&
				(i % 20 == 0) )
			{
				break;
			}
			i += 20;
		}
		long end = System.currentTimeMillis();
		System.out.println(i);
		System.out.println(end-begin + "ms");
	}	
}

Java solution to Project Euler Problem 4

Problem 4:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Running time: 5ms

Assessment: Despite finishing quickly, this is a bad solution, because it returns the first time it reaches a palindrome. The problem is that there’s no guarantee that the return value is, in fact, the correct answer. Using different input criteria could easily break it. It so happens that the returned value of this code is correct, but this was more luck than skill.

public class Problem004
{
	private static int createPalindrome()
	{
		int total = 0;
		for (int i = 999; i >= 900; i--)
		{
			for (int j = 999; j >= 900; j--)
			{
				total = i * j;
				String s = Integer.toString(total);
				String s2 = new StringBuffer(s).reverse().toString();
				if (s.equals(s2))
					return total;
			}
		}
		return 0;
	}
 
	public static void main(String[] args)
	{
		long begin = System.currentTimeMillis();
		int answer = createPalindrome();
		long end = System.currentTimeMillis();
		System.out.println(answer);
		System.out.println(end-begin + "ms");
	}
}

Java solution to Project Euler Problem 3

Problem 3:

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

What is the largest prime factor of the number 600851475143 ?

Running time: 2ms

Assessment: This was when I switched from C++ to Java, and this solution is a lot better than my C++ solution. This one finishes, and finishes pretty quickly.

public class Problem003b
{
	public static void main(String[] args)
	{
		long begin = System.currentTimeMillis();
		long n=600851475143L;
		for (long i = 2; i <= n; i++)
		{
			if (n % i==0)
			{
				System.out.println(i);
				n = n / i;
				i = 2;
			}
		}
		long end = System.currentTimeMillis();
		System.out.println(end-begin + "ms");
	}
}

C++ solution to Project Euler Problem 3

Problem 3:

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

What is the largest prime factor of the number 600851475143 ?

Running time: Unknown, never finishes

Assessment: Brute forced, naive, ugly, doesn’t finish. It was really hard to post this as-is without self-editing to make me look less like a nub, but the point of posting these is to show the evolution of thought processes and problem-solving over time.

The answer is displayed more or less right away, but the code never exits, so you’re not sure if the last answer displayed before you get annoyed and hit Ctrl-C is the correct one. (It is.) I never had the patience to let it finish, let alone measure the runtime. I would approach the problem completely differently–as you’ll see in some of the later factorization examples–if I were to re-write it today.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. long long is_prime(long long n)
  5. {
  6. 	//returns 0 if not prime, 1 if prime
  7. 	if (n % 2 == 0)
  8. 		return 0;		// is even, therefore not prime
  9. 	for (long long i = 3; i <= ((n / 2) + 1); i += 2)	// Skip all the even numbers
  10. 	{
  11. 		if (n % i == 0)
  12. 			return 0;	//not prime
  13. 	}
  14. 	return 1;
  15. }
  16.  
  17. long long find_bigprime(long long n)
  18. {
  19. 	long long factor = 0;
  20. 	for(long long i = 3; i <= n; i += 2)
  21. 	{
  22. 		if (n % i == 0)
  23. 		{
  24. 			if (is_prime(i))
  25. 			{
  26. 				factor = i;
  27. 				cout << factor << endl;
  28. 			}
  29. 		}
  30. 	}
  31. 	return factor;
  32. }
  33.  
  34. int main()
  35. {
  36. 	long long input = 600851475143;
  37. 	cout << find_bigprime(input);
  38.  
  39. 	return 0;
  40. }

C++ solution to Project Euler Problem 2

Problem 2:

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.

Running time: Unknown

Assessment: Inelegant.

This took me an embarrassingly long time to get right. With the exception of Problem 1, I hadn’t written any code in a few years. As noted before, I hadn’t started measuring execution time yet, so I’m not sure how long it took to run, but it’s basically instantaneous.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. 	int total = 0;
  7.  
  8. 	//3 variables to create the Fibonacci sequence from 1 to X
  9. 	int one = 1;
  10. 	int two = 2;
  11. 	int three = 0;
  12.  
  13. 	while (two <= 4000000)
  14. 	{
  15. 		three = one + two;
  16. 		if (two % 2 == 0)
  17. 			total += two;
  18. 		one = two;
  19. 		two = three;
  20. 	}
  21.  
  22. 	cout << total;
  23.  
  24. 	return 0;
  25. }

C++ solution to Project Euler Problem 1

Problem 1:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Running time: Unknown

Assessment: First code I’d written in 7-8 years. I hadn’t started measuring execution time yet, so I’m not sure how long it took to run, but it’s basically instantaneous.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. 	int total = 0;
  7. 	for (int i = 0; i < 1000; i++)
  8. 	{
  9. 		if( (i % 3 == 0) || (i % 5 == 0) )
  10. 			total += i;
  11. 	}
  12. 	cout << total;	
  13. 	return 0;
  14. }