Solving Project Euler #45

https://news.ycombinator.com/rss Hits: 1
Summary

Solving Project Euler #45 Nov 14 2025 Table of contents Introduction Project Euler is a repository of fantastic computational problems that require a modest to heavy degree of mathematical thinking. As someone who enjoys programming and math, I love Project Euler. In this post we'll discuss a solution to a fun problem of theirs that I solved the other day, problem 45. But first, a disclaimer: From the Project Euler FAQ: "[...] the rule about sharing solutions outside of Project Euler does not apply to the first one-hundred problems, as long as any discussion clearly aims to instruct methods, not just provide answers, and does not directly threaten to undermine the enjoyment of solving later problems. Problems 1 to 100 provide a wealth of helpful introductory teaching material and if you are able to respect our requirements, then we give permission for those problems and their solutions to be discussed elsewhere." Emphasis mine. With that noted, let's discuss the problem. Problem statement Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: where is a positive integer. So, for example, . It can be verified that . Find the next triangle number that is also pentagonal and hexagonal. Feel free to try and solve this yourself in another tab. Maybe you'll end up with a different solution than mine, in which case I'd love to hear about it 馃檪 Sketching out a solution We have two equations in three variables, and some very useful domain constraints. Let's say the triangular number equals the pentagonal number equals the hexagonal number. And so: We also have some useful domain constraints. are positive integers, which gives us these inequalities Expand out the equations: Expressing t in terms of h Let's focus on the first one: where . The only way for two integers to satisfy that last equation while both being positive is for them to be equal to each other, and so we get . We've turned one of our variables into a function of the other, ! Let's ...

First seen: 2025-11-16 01:56

Last seen: 2025-11-16 01:56