Python Forum
recursive procedure(total beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursive procedure(total beginner)
#1
Hello guys , for my homeworks i need to create a recursive procedure for x_(k+1)=p(x_k)-x_k. x_k is some number and p(x_k) is defined as the sum of all number that can divide x_k. At the end the code for the sum looks like this. x_k=sumdivisorGenerator(n)


def divisorGenerator(n):
large_divisors = []
for i in xrange(1, int(math.sqrt(n) + 1)):
if n % i == 0:
yield i
if i*i != n:
large_divisors.append(n / i)
for divisor in reversed(large_divisors):
yield divisor
def sumdivisorGenerator(n):
print sum (list(divisorGenerator(n)))


The recursive procedure x_(k+1)=p(x_k)-x_k has to end, if 0 is reached or put an error if a number gets hit twice and put out the length of the recurive procedure if its succesfully. I just have the problem that i dont know how to do this.".

After i tried to define x_(k+1)=p(x_k)-x_k(catalansequence(n)) iam already gettting an error when i try to use numbers because "print(sumdivisorGenerator(n)-n)" (TypeError: unsupported operand parent(s) for -: '<type 'NoneType'>' and 'Integer Ring')

def catalansequence(n):
print(sumdivisorGenerator(n)-n)

I would be really grateful if someone could help me with my homwork.(sorry for my bad english)
Reply
#2
Read about the return statement: https://docs.python.org/3/reference/simp...-statement
Reply
#3
i have changed my code now, i did not know the difference between "print" and "return"
︠


def divisorGenerator(n):
    large_divisors = []
    for i in xrange(1, int(math.sqrt(n) + 1)):
        if n % i == 0:
            yield i
            if i*i != n:
                large_divisors.append(n / i)
    for divisor in reversed(large_divisors):
        yield divisor
def  sumdivisorGenerator(n):
    return sum (list(divisorGenerator(n)))

def catalansequence(n):
    return (sumdivisorGenerator(n)-n)

def length(n):
    if sumdivisorGenerator(n)==0:
        return 0
    
Now i need to define my length (n). If sumdivisorGenerator(0) the result=0. But now i need this procedure to break if a number gets hit twice or put out the length(n). I would be really grateful for some hint.
Reply
#4
(Dec-13-2017, 10:10 AM)boris602 Wrote: i have changed my code now, i did not know the difference between "print" and "return"
︠


def divisorGenerator(n):
    large_divisors = []
    for i in xrange(1, int(math.sqrt(n) + 1)):
        if n % i == 0:
            yield i
            if i*i != n:
                large_divisors.append(n / i)
    for divisor in reversed(large_divisors):
        yield divisor
def  sumdivisorGenerator(n):
    return sum (list(divisorGenerator(n)))

def catalansequence(n):
    return (sumdivisorGenerator(n)-n)

def length(n):
    if sumdivisorGenerator(n)==0:
        return 0
    
Now i need to define my length (n). If sumdivisorGenerator(0) the result=0. But now i need this procedure to break if a number gets hit twice or put out the length(n). I would be really grateful for some hint.

I don't quite understand what length(n) is supposed to output. Is it the number of times sumdivisorGenerator is called? Is it the length of the list you created in sumdivisorGenerator? I assume it's this, that length(n) should output the number of numbers that can divide n.

Python includes a length function, called len https://docs.python.org/3.6/library/functions.html#len that operates on lists just like the sum function does.
Reply
#5
The length is defined how long this procedure "x_(k+1)=p(x_k)-x_k" runs until i reach 0. So for example for x_0=7 , x_1 is =1 and x_2 is =0. So the length must be 2, but i dont know how i can code this and some numbers like x_0=6 always stay 6 and have to be breaked.
Reply
#6
(Dec-13-2017, 12:59 PM)boris602 Wrote: The length is defined how long this procedure "x_(k+1)=p(x_k)-x_k" runs until i reach 0. So for example for x_0=7 , x_1 is =1 and x_2 is =0. So the length must be 2, but i dont know how i can code this and some numbers like x_0=6 always stay 6 and have to be breaked.

I think I understand now, you're generating a sequence Xk, Xk+1, Xk+2, ... where Xy = p(Xy-1) - Xy-1.

Generating the sequence is recursive and you stop when Xy = 0. So how many times do you calculate p(Xy-1) - Xy-1 before it returns zero and how do you stop when p(x) - x = x.

So the divisorGenerator method (which is a generator) can remember information it's calculated from previous calls. You're using that by storing things in large_divisors. Can you use it (the fact that generators remember information between calls) to keep track of things it has seen? Right now, you're just summing the list generated by divisorGenerator. Maybe you have to look at the list returned by divisorGenerator and add logic based on what it's returning.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating Disassembler for a bin file(total beginner) SoulsKeeper 1 2,460 Sep-04-2018, 04:15 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020