Tag Archives: C++

C++ solution to Project Euler Problem 10

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.

Running time: ~5 secs

Assessment: This solution is in C++ for some reason. Oh well. Anyway, it works fine, but like most of my other early factorization solutions, it doesn’t memoize anything, which means lots of redundant work, which makes for very long execution times. It’s pretty bad all around.

#include <iostream>
#include <cmath>
 
using namespace std;
 
long long is_prime(long long n)
{
	//returns 0 if not prime, 1 if prime
	if (n % 2 == 0)
		return 0;
	for (long long i = 3; i <= (pow(n,0.5)); i += 2)
	{
		if (n % i == 0)
			return 0;
	}
	return 1;
}
 
int main()
{
	unsigned long long sum = 0;
 
	for (int i = 3; i < 2000000; i += 2)
	{
		if (is_prime(i))
		{
			sum += i;
		}
	}
 
	cout << sum + 2;
 
	return 0;
}

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. }