A mysterious recursion example?

Revision en2, by touhidurrr, 2021-07-15 02:40:28

Hi. I wanted to make an example of splitting up recursive functions on python because python returns error if the level of a recursive function gets to deep. And somehow got this funny thing that returns the higest power of 2 from 1 to n given n. Not sure how exactly it happens. Can anyone explain it Mathematically maybe?

def example(n):
  if n < 2: return 1
  return example(n/2) + example(n/2)

def example2(n):
  if n < 2: return 1
  return example2(n/4) + example2(n/4) + example2(n/4) + example2(n/4)

print(example(100), example2(100))
# prints 64 64

print(example(1600), example2(1600))
# prints 1024 1024

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English touhidurrr 2021-07-15 02:40:28 4 Tiny change: ' given n. Don't sure how' -> ' given n. Not sure how'
en1 English touhidurrr 2021-07-15 02:39:25 683 Initial revision (published)