аватар question@mail.ru · 01.01.1970 03:00

The use of recursion is more than 1000 times

I am a beginner in programming on Python (my 1 programming language). He solved the problem No. 12 from the Euler project. I used a recursion in functions, but unfortunately, to solve the problem, I will have to use the recursion more than 1000 times. I would like to know how to solve this problem as before using a recursion in order to know about it in the future.

how to improve the code and what do you think about my names of variables and functions?

This is how the task is formed by the sequence of triangular numbers by adding natural numbers. For example, the 7th triangular number is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten triangular numbers:

1, 6, 10, 15, 21, 28, 36, 45, 55, ...

We list the divisors of the first seven triangular numbers: 1: 1 3: 1, 3 6: 1, 2, 3, 6 10: 6 10: 6 10: 6 10: 6 10: 6 10: 6 10: 6 10: 6 10: 6 10: 6 10 10: 6 10: 6 10 10: 6 10 10: 6 10 10: 6 10 10: 6 10 10: 6 10: 6 10 10: 6 10 10: 6 10 10 1, 2, 5, 10 15: 1, 3, 5, 15 21: 1, 3, 7, 21 28: 1, 2, 4, 7, 14, 28 As we see, 28 is the first triangular number, which has more than five divisors.

What is the first triangular number that has more than five hundred divisors?

   def   tringle_number  ( x ):   if  x ==  1 :   retu   1    else :   Retu  X + Tringle_number (x-  1 )   for  i  in   range  ( 1 ,  900 ):  a.Append (tringle_number (i))   print  (a) need =  0  all_number = [ 1 ] count =  0    for  item  in  a:  number =  0    for  j  in   range  ( 1 ,  100,000 ):   it  item % j ==  0 :  conside = J Number +=  1    if  number == :   for  items  in  all_number:  need +=  1    if  need ==  1 :  all_number.append (item)   else :   continue    else :   Continue    print  (all_number [ 1 ]) r  
аватар answer@mail.ru · 01.01.1970 03:00

by default, the size of the stack in the python is limited to 1000 calls. This is usually enough. If you write a recursive algorithm (you like it so much, or you will port a code with LISP, or you are a fan), then this restriction can seriously spoil the mood.

The limitation can be loosened by a challenge. This challenge will increase the limit, but it is not omnipotent. In Python, the number of calls is also limited by the amount of memory that is highlighted for stack. From the inside of the program you can not influence the size of the stack. If the stack is overflowing, then the operating system stops the interpreter.

the choice is sad: either the program will reproach the limit of the interpreter itself and get the exception

  in  comparison    

or the program will be stopped with an error

  Segtion Fault (Core Dumped)     

In the second case, nothing can be done. In the first case, there is an opportunity to recover after an error - and this is the reason why it seems to be useless, the restriction exists.

for example: on my CPYTHON system it breaks at a stack depth of about 21817.

If this is not enough for you, then look. He almost does not use hardware. It also needs to be installed sys.setrecursionlimit . I set a billion. The depth of recursion of 10 million is not a problem.

for 100 million did not have enough 32GB memory. In The Stackless Python, one recursive call takes about 790 bytes. 40 million calls will take 32GB memory.

The last restriction will be difficult to overcome. If you need more, look for another interpreter (let me know how you find) or another language (for example, with optimization support).

Latest

Similar