0

So I have this math formula that has two inputs (two numbers that you have to plug in, positive integers). The names for the terms are "diagonal" and "term (n)". The equation is

( n(n+1)(n+2)...(n+ (diagonal-2)) )/ (diagonal-1)!

So essentially you plug in two numbers (diagonal, and term) and it should spit out a number. So I wrote a program in python, but it always returns 0.

import math
diagonal = input("What diagonal do you want to see?")
term = input("What term do you want to see?")
product= term
for i in range (term + (diagonal - 2)):
 product = ((product * (i+1))/(math.factorial(diagonal - 1)))
print(product)

To test this when you plug in number 4 for both the term and diagonal you should get 20.

EDIT: I tried the different methods posted and now it return 80 when 4,4 is plug in. import math

diagonal = input("What diagonal do you want to see?")
term = input("What term do you want to see?")
product= float(term)
for i in range (term, term + (diagonal - 2)+1):
 product = (product * (i))
product /= math.factorial(diagonal - 1)
print product
5
  • range(term + (diagonal - 2)) looks wrong. It should be range(term, term + (diagonal - 2) + 1)` (and use i instead of i+1) Commented Jul 20, 2015 at 1:48
  • if I may, your formula looks like (n+d-2)! / ((d-1)! * (n-1)!). Commented Jul 20, 2015 at 1:49
  • I tired the njzk2 method and it still return the wrong answer Commented Jul 20, 2015 at 1:53
  • product= float(term) didn't see that. of course, if you look at the actual expression, there is now 2 term, so just remove one. Also, did you try the second thing I posted? I don't think you need all that looping. Trivially: product = math.factorial(term + diagonal + 2) / (math.factorial(diagonal - 1) * math.factorial(term - 1)). Commented Jul 20, 2015 at 3:20
  • I tried the second comment you posted and it works except it is (term+ diagonal -2). Thank a lot Commented Jul 20, 2015 at 3:35

3 Answers 3

4

I don't think you want to divide by the factorial for every iteration. Do that after your loop.

for i in range (term + (diagonal - 2)):
 product = product * (i+1)
product /= math.factorial(diagonal - 1)
answered Jul 20, 2015 at 1:46
Sign up to request clarification or add additional context in comments.

1 Comment

Well, now you might have to do some debugging. By "wrong answer" I presume you mean it now returns something other than 0. So that's one problem fixed. But I'm not sure that your decomposition of your formula into a loop is correct and you'll probably have to work that out yourself.
1

Your algorithm is incorrect, especially when dividing by the factorial.

If you are sure that is what you want to do, you may want to make product a float or you will always get zero since your first divide is less than 1...

product = float(term)
answered Jul 20, 2015 at 1:52

Comments

0

Here's a functional approach to your problem.

from operator import mul
product = lambda lo, hi: reduce(mul, xrange(lo, hi))
formula = lambda n, d: product(n, n+d-1)/product(1, d)
print formula(4, 4)

20

EDIT
The problem with your code is that you are multiplying by the initial value of term twice (hence why you are getting 80 instead of 20 when the term and diagonal are both 4). To fix this change the line product= float(term) to product = 1.

answered Jul 20, 2015 at 5:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.