Second attempt while more efficient when it comes to the large number it's too slow and the program runs out of memory.