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 S-number to be a natural number, n, that is a perfect square and its square root can be obtained by splitting the decimal representation of n into 2 or more numbers then adding the numbers.

For example, 81 is an S-number because 81=8+1.
6724 is an S-number: 6724=6+72+4.
8281 is an S-number: 8281=8+2+81=82+8+1.
9801 is an S-number: 9801=98+0+1.

Further we define T(N) to be the sum of all S numbers nN. You are given T(104)=41333.

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

Popular posts from this blog

Count Binary Substrings

Count Vowels Permutation: Standard DP

Maximum Number of Balloons