1 Replies - 1670 Views - Last Post: 29 December 2011 - 05:18 PM

#1 atik97   User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Sum of any series

Posted 20 March 2009 - 02:25 PM

Description:
puts "Enter Number of Terms"
n=gets.chomp
n=n.to_f
puts "Enter 1st Term"
a=gets.chomp
a=a.to_f
puts "Enter 2nd Term"
b=gets.chomp
b=b.to_f
puts "Enter 3rd Term"
c=gets.chomp
c=c.to_f
d1=b-a
d2=c-b
r1=b/a
r2=c/b

if d1==d2
  s=(n/2)*(2*a+(n-1)*d1)
  puts "The sum of the series is "+s.to_s

elsif r1==r2
  s=(a*(r1**n-1))/(r1-1)
  puts "The sum of the series is "+s.to_s

else
  puts "This is not a valid series!"
end



Is This A Good Question/Topic? 0
  • +

Replies To: Sum of any series

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Sum of any series

Posted 29 December 2011 - 05:18 PM

Note that this only calculates the sum for arithmetic series, meaning series for which the the next term is found by adding or subtracting a constant. Please change the title to reflect this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1