Could someone help me with this. How do i make a recursive program that returns sum of 1**3 + 2**3 + ... + n**3?
doeman 0 Newbie Poster
Recommended Answers
Jump to PostHere is another way you can skin it:
def sum_cube(n, sum=0): if n > 0: x = n**3 return sum_cube(n-1, sum+x) else: return sum print sum_cube(5) # 225
Jump to PostHere's another recursive way although it is kind of cheating:
>>> sum_cube = lambda N: eval(["0","(N*N*N) + sum_cube(N-1)"][N>0]) >>> sum_cube(10) 3025
All 6 Replies
Gribouillis 1,391 Programming Explorer Team Colleague
ZZucker 342 Practically a Master Poster
doeman 0 Newbie Poster
ZZucker 342 Practically a Master Poster
BearofNH 104 Posting Whiz
ZZucker 342 Practically a Master Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.