Using recursion more than 1000 times
📁 example, AI, Beginner, project
I'm new to programming in Python (my 1st programming language). I was solving problem #12 from Project Euler. I used recursion in the function, but unfortunately, to solve the problem, I would have to use recursion more than 1000 times. I would like to know how to solve this problem while still using recursion, so that I know about it in the future.
How can I improve the code and what do you think about my variable and function names?
Here is the problem statement:
The sequence of triangle numbers is generated by adding the natural numbers. For example, the 7th triangle number is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the divisors of the first seven triangle numbers: 1: 1 3: 1, 3 6: 1, 2, 3, 6 10: 1, 2, 5, 10 15: 1, 3
To convert arbitrary text into "01"-strings (bits):
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'): bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:] return bits.zfill(8 * ((len(bits) + 7) // 8))And back:
def text_from_bits(bits, encoding='utf-8'): Log in to leave an answer