First of all, I suggest checking your performance with
BenchmarkTools. This way you can see if you are making any performance improvements with your code changes.
If I do that with
n = 1000000, you clock in at
345.713 ms (4348978 allocations: 422.86 MiB) … wait what, 4 million allocations for counting something?
If you have a function like
iterate_3 the usual way to do something like that is
for a in b
# if we are still here that means the thing must be fine!
Currently you use an array, but you push a value you don’t care about (
1000) to the array, and you only ever check if anything is there.
Also, why do you reverse your array? This allocates a new array every time you do it. If you really need to reverse it, you can use
reverse!, which reverses in place without allocating a new array. Speaking of which, the same is possible for
digits -> digits!.
All of that aside. You want to count through roughly
1e19 numbers. Lets say you can do each check in 1ns (not true, but let’s assume). That means you need
1e10 seconds or roughly 300 years to do this.
This means (as it is almost always the case with Euler Project problems) you need to think of a clever algorithm to do this. Simply going through every number won’t cut it.
As a pointer: you go from
19999999999999999999. By just looking at the last number, I know that you will be checking roughly
999999999999999999 numbers too many, because if your number starts with
19 we are done already. You can only go up to
180.... Also, what’s up with
20000000000000000000? You stop just shy of that, but this number is okay, no?
Instead of thinking about the performance of your code snippet, you’ll have to look more closely at your algorithm first.