You are getting NZEC because you are calculating the complete value x = a ** a. Since the value of a is large (upto 10^9) the value a ** a will be very large and have billions of digits. Python stores this value as a string and as it cannot store such a large string in memory so it raises a memory exception which causes the NZEC error.