fixed the program to use less memory

fixed a bug in the program where range function is used instead of a generator
that caused program to use huge amount of memory when large numbers are used.