PDA

View Full Version : Tetrahedral number



blackevil
March 9th, 2010, 05:29 PM
I have been given a task of writing a program in Python for Tetrahedral number in recursive function form ie :


code:

def tet(n):
if n<=*:
return *
else:
return * + tet(**)



numbers are:

1 1
2 4
3 10
4 20
5 35




What is the nth term?

kernco
March 9th, 2010, 05:33 PM
Hmm...

1 1 = 1^2
2 4 = 2^2
3 10 = 3^2 + 1
4 20 = 4^2 + 4
5 35 = 5^2 + 10

Looks like f(n) = n^2 + f(n-2)?

blackevil
March 9th, 2010, 05:56 PM
Hmm...

1 1 = 1^2
2 4 = 2^2
3 10 = 3^2 + 1
4 20 = 4^2 + 4
5 35 = 5^2 + 10

Looks like f(n) = n^2 + f(n-2)?




it works for 1,2,3

but didnt work for the rest?

kernco
March 9th, 2010, 06:05 PM
it works for 1,2,3

but didnt work for the rest?

Well the equation I posted definitely works for 4 and 5, you can do the math to confirm it. So it must be that your code isn't correctly representing the equation. Here's how I'd fill in the blanks in the code you posted.

def tet(n):
if n<=2:
return n**2
else:
return n**2 + tet(n-2)

Can+~
March 9th, 2010, 06:07 PM
So we're answering homeworks now?

kernco
March 9th, 2010, 06:09 PM
So we're answering homeworks now?

I need something to pass the time at work.

MadCow108
March 9th, 2010, 07:02 PM
the next numbers are: 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969,..

to find the formula think about what a tetraeder is
(or use the obvious lazy route I choose ;) )

kernco
March 9th, 2010, 07:05 PM
Uh oh! Guess my equation fails for n>5

LKjell
March 9th, 2010, 08:19 PM
Go to wikipedia you get a formula there.

wmcbrine
March 9th, 2010, 11:16 PM
Please use code tags, especially around Python code, where indentation is significant. In this case it's possible to infer the intended indent, but it isn't always.

And yeah, stop posting homework.