Project Euler programming challenges: Am I missing something?

Click For Summary

Discussion Overview

The discussion revolves around the programming challenges presented by Project Euler, specifically focusing on problem 1, which involves finding the sum of all multiples of 3 or 5 below 1000. Participants explore various approaches to solve the problem, including brute force methods and mathematical optimizations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants suggest that the problem seems straightforward but may require a more efficient algorithm than brute force.
  • One participant introduces Gauss's formula for summing integers as a potential optimization for calculating the sum of multiples.
  • Another participant points out that while using Gauss's formula, one must consider the overlap of multiples of 3 and 5, which could lead to double counting.
  • A participant provides a Perl script as an alternative method to calculate the sum, although they express doubt about its acceptance in the Project Euler community.
  • There is a discussion about the efficiency of brute force methods versus more sophisticated algorithms, with some participants expressing disdain for brute force approaches.
  • Participants engage in a back-and-forth about the correctness of the proposed algorithms and the need to account for intersections in the sums of multiples.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of brute force methods versus optimized algorithms. There is no consensus on the best approach, and the discussion remains unresolved regarding the correctness of the proposed solutions.

Contextual Notes

Some participants suggest testing algorithms with smaller numbers to verify correctness, indicating that assumptions about the problem's scope may need clarification. The discussion also highlights the potential for miscalculations when not accounting for overlapping multiples.

  • #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
822
  • · 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