Python Pure recursion - Divisor - One input -


what recursive call (or inductive steps) function returns number of integers 1 n, evenly divide n. idea concieve pure recursive code in python function. no 'for' or 'while' loops, neither modules can used. function num_of_divisors(42) returns 8, representing 1, 2, 3, 6, 7, 14, 21, , 42 divisors of 42.

def num_of_divisors(n):     return sum(1 if n % i==0 else 0 in range(((n+1)**0.5)//1) 

good luck explaining teacher!

if can't use for loops (?????????) impossible without simulating one.

def stupid_num_of_divisors_assigned_by_shortsighted_teacher(n, loop_num=1):     """i had copy stack overflow because it's such     inane restriction it's harmful learning language     """      if loop_num <= (n+1) ** 0.5:         if n % loop_num == 0:             return 2 + \                 stupid_num_of_divisors_assigned_by_shortsighted_teacher(n, loop_num+1)         else:             return stupid_num_of_divisors_assigned_by_shortsighted_teacher(n, loop_num+1)     else:         if n % loop_num == 0:             return 1 

bonus points: explain why you're adding 2 in first conditional, 1 in second conditional!


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -