Project Euler programming challenges: Am I missing something?

Click For Summary
Project Euler challenges are designed to encourage efficient algorithm development rather than brute force solutions. The discussion highlights the importance of using mathematical formulas, like Gauss's sum, to optimize calculations for problems such as finding the sum of multiples of 3 or 5 below a certain number. Participants explore the correct implementation of these formulas while addressing potential overlaps in multiples, emphasizing the need for careful consideration of set theory. The conversation also touches on improving code for flexibility and efficiency, suggesting enhancements like using unsigned integers and accommodating different sets of multiples. Overall, the thread underscores the balance between simplicity and sophistication in problem-solving approaches.
  • #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 3 ·
Replies
3
Views
566
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
7K