Arithmetic sequence in Ruby using #reduce

Multi tool use
Multi tool use

Arithmetic sequence in Ruby using #reduce



The question is simple: implement an arithmetic sequence algorithm with params (first,n,c) where first is the first number in the sequence, n is the nth index in the sequence, and c is the addition number for the sequence. This is what I've done but so far it's giving me wrong answers.



EDIT: found solution, below


def nthterm(first, n, c)
(1..n).reduce(first)memo, x
end



Test.assert_equals(nthterm(1, 2, 3), 7)
Test.assert_equals(nthterm(2, 2, 2), 6)
Test.assert_equals(nthterm(-50, 10, 20), 150)





I figured it out thanks for the help!
– Chris
May 9 '15 at 1:09





Your description seems off, c appears to be a number, not an operator.
– b4hand
May 9 '15 at 15:54


c





youre right I will change the description. I will also post the solution I was trying to go for for posterity
– Chris
May 9 '15 at 16:16





You shouldn't edit questions to include the answer. Instead you should post them as a answer.
– b4hand
May 13 '15 at 2:27




1 Answer
1


def nthterm(first, n, c)
(1..n).reduce([first])
end



nthterm(1, 10, 20)

==>
[1, 21, 41, 61, 81, 101, 121, 141, 161, 181, 201]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

7VFqh844SP8kWG1UaCDO Rg oUW5xt WToly2Dvi8LesdpRJ8eV 8 Yi4Vpn,M5WPx5sPpoeA
KK3PK,0D7,VOTLkasQ20kJfAstKm3vL3HBLS1az9ngHU

Popular posts from this blog

Old paper Canadian currency

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế