Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The smallest code that gives the area between the curve p(x) = a0 + a1*x + a2*x2 + ..., the line y = 0, the line x = 0 and the line x = C

(i.e. something like this:

wanted area)

You can assume that p(x) >= 0 for x < C (bonus points if your code works for negative values of p(x)).

Input

C, a0, a1, ...

Output

a real number - the area

Example 1:

input: 2, 0, 1
output: 2.0

Examlpe 2:

input: 3.0, 0, 0.0, 2
output: 18

UPDATE:

  • C > 0 is also assumed
  • the area is between the curve, y=0, x=C and x = 0
  • the input can be a list of any form; not necessarily comma separated.
  • the output can be a real of any form (thus, '18' is a valid output, as is '18.0')
share|improve this question
1  
Since the answer is going to be "infinite" for almost any input, I think you've misstated the problem. – Peter Taylor Feb 3 '11 at 21:37
Should the input be read from standard input as a comma-separated string? Or can we write a function which takes a list of floats as an argument? – sepp2k Feb 3 '11 at 21:47
Do you mean between x=0, x=C, y=0, and the curve? – Keith Randall Feb 3 '11 at 22:06
2  
@Peter: I don't think so. He shows a picture of an inverse (the integral of which would diverge), but the function he specifies is a polynomial. The definite integral over [0,C) should be well defined and finite for finite coefficients. – dmckee Feb 4 '11 at 1:20
1  
@dmckee, I had noticed that, but my point was more that he was integrating a polynomial from -\infty to C, and for any non-trivial polynomial that diverges. The question has now been amended to fix this. – Peter Taylor Feb 4 '11 at 7:11
show 5 more comments

5 Answers

up vote 2 down vote accepted

Mathematica: 48 Chars

.

Sum[#[[i+1]]#[[1]]^i/i,{i,Length@#-1}]&@Input[]
share|improve this answer

Python - 71 63 chars:

a=input()
print sum(1.*a[i]*a[0]**i/i for i in range(1,len(a)))

It's a simple integration of a polynomial function between 0 and C. And I haven't tested it, but I'm quite sure it works for negative values.

share|improve this answer
Learned something new about input() today:) – st0le Feb 4 '11 at 4:39

J, 26 characters

f=:3 :'((1}.y)&p.d._1)0{y'

eg.

   f 2 0 1
2
   f 3 0 0 2
18
share|improve this answer
Neat! I can't find a way to make it more tacit. That d. being a conjunction doesn't make it very easy to my novice J skills. – J B Feb 12 '11 at 15:13
@J-B: Yes, that d. is a "problem" for me too. :) – Eelvex Feb 12 '11 at 15:21

Haskell, 85 characters

f(c:l)=sum.map(\(i,x)->x*c**i/i)$zip[1..]l
main=getLine>>=print.f.read.('[':).(++"]")
share|improve this answer
3  
Whoever downvoted, please leave a comment. – Eelvex Feb 5 '11 at 11:42
The question isn't as strict as you treat it. You could definitely simplify the input code, and possibly do away with explicit I/O altogether. – J B Feb 12 '11 at 15:19

Ruby, 65 chars

i=s=0
c=gets(q=",").to_f
$<.each(q){|a|s+=a.to_f*c**(i+=1)/i}
p s

The code reads until the end of input, not the end of the line. So you need to hit Ctrl+d to terminate input (pipe the input in using echo or from a file).

share|improve this answer
1  
i think assigning the "," to a variable will help...how about this c=gets(q=",").to_f and $<.each(q){|a|s+=a.to_f*c**(i+=1)/i}, saves one char.... – st0le Feb 4 '11 at 4:31
@st0le: Very nice. Thanks. – sepp2k Feb 4 '11 at 4:32
Assigning "," (or ?,, which is even shorter) to $/ allows you to omit the argument to $<.each. And $<.map is one character shorter than $<.each. ;) – Ventero Feb 12 '11 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.