Project Euler programming challenges: Am I missing something?

Click For Summary
SUMMARY

The discussion revolves around solving Project Euler's Problem 1, which requires calculating the sum of all multiples of 3 or 5 below 1000. Participants explore both brute force and optimized methods, highlighting the use of Gauss's Sum of Integers Formula. The optimized solution significantly reduces computation time, demonstrating the importance of algorithm efficiency in programming challenges. The final optimized formula accounts for overlaps in multiples, ensuring accurate results.

PREREQUISITES
  • Understanding of basic programming concepts (C++ syntax and structure)
  • Familiarity with mathematical series and summation techniques
  • Knowledge of algorithm optimization techniques
  • Experience with time complexity analysis
NEXT STEPS
  • Learn about Gauss's Sum of Integers Formula and its applications
  • Explore advanced algorithm optimization techniques
  • Study set theory and its relevance in programming challenges
  • Investigate other Project Euler problems for further practice
USEFUL FOR

Programmers, algorithm enthusiasts, and students looking to enhance their problem-solving skills through mathematical challenges and efficient coding practices.

  • #31
Jamin2112 said:
Right now I'm figuring out a non-brute-force way. Here's pure brute:
When you apply brute force you need to ensure that you are apply that brute force to the right problem. Here you are applying brute force to solve the wrong problem. Your algorithm finds the largest palindrome that is smaller than 999*999=998001. You are not finding the largest palindrome that is the product of two three digit numbers, which is 993*913=906609.

Your program's output is 997799. This has a rather easy factorization: It is product of two primes, 11 and 90709. Since both are prime and neither is a three digit number, this does not qualify as the answer to the problem. Here are two different approaches to the problem:
  • Use a double loop that walks through all pairs of three digit numbers. Compute their product. Save the product as the largest palindromic product if the product is larger than your largest palindromic product and if it's a palindrome.

  • Walk downward through the palindromic numbers, starting at 999999 (or 997799 to be a bit smarter). (You'll need an algorithm to form the next smallest palindromic number.) Compute the prime factorization (including repetitions) of the current palindromic number. Stop when the prime factorization can be recombined to form a pair of three digit numbers.

The first approach is a bit more brute forcish, but it's probably going to be faster than the second because factorization is a slow algorithm, as is the recombination of these prime factors.
 
Last edited:
Technology news on Phys.org
  • #32
D H said:
When you apply brute force you need to ensure that you are apply that brute force to the right problem. Here you are applying brute force to solve the wrong problem. Your algorithm finds the largest palindrome that is smaller than 999*999=998001. You are not finding the largest palindrome that is the product of two three digit numbers, which is 993*913=906609.

Your program's output is 997799. This has a rather easy factorization: It is product of two primes, 11 and 90709. Since both are prime and neither is a three digit number, this does not qualify as the answer to the problem. Here are two different approaches to the problem:
  • Use a double loop that walks through all pairs of three digit numbers. Compute their product. Save the product as the largest palindromic product if the product is larger than your largest palindromic product and if it's a palindrome.

  • Walk downward through the palindromic numbers, starting at 999999 (or 997799 to be a bit smarter). (You'll need an algorithm to form the next smallest palindromic number.) Compute the prime factorization (including repetitions) of the current palindromic number. Stop when the prime factorization can be recombined to form a pair of three digit numbers.

The first approach is a bit more brute forcish, but it's probably going to be faster than the second because factorization is a slow algorithm, as is the recombination of these prime factors.

Whoops. I can't believe how much of an idiot I am sometimes.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 3 ·
Replies
3
Views
697
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K