ProjectEuler Problem 719 (some hints, but no spoilers)
Last time that I solved a ProjectEuler (PE) problem was in 2015...
This is their latest problem as of today: https://projecteuler.net/problem=719
Number Splitting
Problem 719
We define an -number to be a natural number, , that is a perfect square and its square root can be obtained by splitting the decimal representation of into 2 or more numbers then adding the numbers.
For example, 81 is an -number because .
6724 is an -number: .
8281 is an -number: .
9801 is an -number: .
Further we define to be the sum of all numbers . You are given .
Find
The rules of PE prevent me from disclosing the solution (they are very picky about it, rightfully so), but I can chat about the overall idea:
First, understand that you're dealing with square root, and that the problem is asking for the solution for N=10^12. Use these two data points to understand a bit of the complexity that you want to achieve (don't try O(N), won't work).
Second, think about how to write some code to, as quick as you can, check whether a number is an S-number. It won't be constant, but the numbers won't be that big either, and the complexity should be a polynomial of Log(number). Think recursively here to solve it. Think pruning the search for a solution (this function has to be fast, as fast as possible).
Finally, please use long, not int.
When you put it all together, you should test against N=10^4 to make sure you're not going to be off by one or so (there are some weird cases when the numbers are small). Then, run against 10^12. It won't be instantaneous, but it should finish within 5min. In my case, it ran in about 90 seconds:
Running time: 00:01:25.1470336
Unfortunately, for the first time in this blog, no code to be shown since I want to respect the rules of engagement stipulated by PE. But I hope that the idea above gives you some direction. Cheers, ACC.
Comments
Post a Comment