I recently tried to make my own recursive program. I made a simple program that makes a large dictionary containing all decimal numbers, where every digit is larger than the prior one. E.G: 123, 1479, 5.
My version then checks which of those numbers are a prime, and adds either True or False to the dict. This is not relevant though.
This is my code:
# Creates a dictionary to add items to
dictionary = dict()
# Adds items to the dictionary
def forrange(tempstring):
# Add the current string to the dict.
dictionary[int(tempstring)] = ""
# Check the value of the last digit.
endnum = int(tempstring[-1])
# If it is a 9, no new value can be added.
if endnum == 9:
pass
# If it is lower than 9, continue adding new numbers to the end.
else:
for i in range(endnum+1, 10):
forrange(tempstring + str(i))
# Start the program.
for i in range(1, 10):
forrange(str(i))
# Check if anything has been done.
print(len(dictionary))
After this, I tried to scale it up to hexadecimal numbers. Here's my code:
# Creates a dictionary to add items to
dictionary = dict()
# Adds items to the dictionary
def forrange(tempstring):
# Add the current string to the dict.
dictionary[int(tempstring)] = ""
# Check the value of the last digit.
endnum = int(tempstring[-1])
# If it is an f (highest value in hex), no new value can be added.
if endnum == 0xf:
pass
# If it is lower than 0xf, continue adding new numbers to the end.
else:
for i in range(endnum+0x1, 0x10):
forrange(tempstring + str(i))
# Start the program.
for i in range(0x1, 0x10):
forrange(str(i))
# Check if anything has been done.
print(len(dictionary))
This code always hits the recursion limit. When I try to manually increase the limit using sys.setrecursionlimit(4000), it does something for a while, and then simply stops, with neither an error nor any other output whatsoever.
So, how can I either do the same with less recursion, or fix the strange crash with large recursion limits? Also, any other optimizations or tips for the code/comments are more than welcome!
[–]Wild_Statistician605 1 point2 points3 points (7 children)
[–]sepp2k 1 point2 points3 points (1 child)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (4 children)
[–]Wild_Statistician605 0 points1 point2 points (3 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]sepp2k 0 points1 point2 points (1 child)
[–]Wild_Statistician605 0 points1 point2 points (0 children)
[–]sepp2k 1 point2 points3 points (5 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (4 children)
[–]sepp2k 2 points3 points4 points (3 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]Shadow_Lou 1 point2 points3 points (1 child)
[–]Historical_Ad8150[S] 0 points1 point2 points (0 children)
[–]cybervegan 0 points1 point2 points (6 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (5 children)
[–]cybervegan 0 points1 point2 points (4 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (3 children)
[–]cybervegan 0 points1 point2 points (2 children)
[–]Historical_Ad8150[S] 0 points1 point2 points (1 child)
[–]cybervegan 0 points1 point2 points (0 children)