Arithmetic Sequences

5

1

An arithmetic sequence is a sequence of numbers of the form a,a+n,a+2n,a+3n, etc. In this case, a and n will both be either integers or decimals.

Given two inputs, a, n, and a top number value, return a list of numbers where the last term is less than or equal to the top number value.

The input can be formatted in any way, as well as the output.

The catch to this problem is dealing with floating point arithmetic errors.
For example, 0.1 + 0.3 * 3 is 1, however this will display 0.99999999999... if you simply go 0.1 + 0.3 * 3. We want 1, not 0.99999999999...

Test Cases

a=2, n=3, top=10: [2, 5, 8]
a=3, n=1.5, top=12: [3, 4.5, 6, 7.5, 9, 10.5, 12]
a=3.14159, n=2.71828, top=10: [3.14159, 5.85987, 8.57815]
a=0.1, n=0.3, top=2: [0.1, 0.4, 0.7, 1, 1.3, 1.6, 1.9]

Shortest code wins. Good luck!

beary605

Posted 2012-06-07T00:53:50.093

Reputation: 3 904

Is this supposed to be a standalone program, or a function? If a standalone program is provided, are there any requirements to the format of the input or output? – breadbox – 2012-06-07T05:11:50.267

Function/standalone is fine. There is no specified input or output: the programmer will state both. – beary605 – 2012-06-07T05:49:45.240

1What do you mean by "floating points" in the first paragraph? If they're actual IEEE754 floating point numbers then most of your test cases don't make sense; so are they strings representing decimal floating point numbers? If so, to what precision? – Peter Taylor – 2012-06-07T09:33:44.287

I changed "floating points" to "decimals". – beary605 – 2012-06-07T15:53:51.467

The input/output part should be specified in the question, not just in the comments. – user unknown – 2012-06-08T13:41:32.617

Answers

5

APL (22)

{⍵≤T:⍵,∇⍵+N⋄⍬}⊃A N T←⎕

Reads input from stdin as three whitespace-separated numbers.

This is actually shorter than using the index generator.

marinus

Posted 2012-06-07T00:53:50.093

Reputation: 30 224

This problem was actually a fail. Using the print command, or the equivalent of it, rounded off floating points, so the challenge was not a challenge at all. – beary605 – 2012-06-09T02:44:15.917

6

Non-golfed haskell, 32

f a n t=takeWhile(<=t)[a,a+n..]

I'd love to change takeWhile to take, but integer division can't be done with /, it needs to be done with div. If I go with / then I need to do implicit conversion which makes it that much longer.

shiona

Posted 2012-06-07T00:53:50.093

Reputation: 2 889

That only works without the floating-point problems if called as f :: Rational -> Rational -> Rational -> Rational rather than e.g. Double -> Double -> Double -> Double. But I doubt output in the form [1 % 10,2 % 5,7 % 10,1 % 1,13 % 10,8 % 5,19 % 10] will be accepted. – ceased to turn counterclockwis – 2012-06-07T20:17:20.350

@leftaroundabout If it was my problem, I'd accept that output! (But it's not.) – breadbox – 2012-06-08T03:32:41.830

5

J, 34 33 31 characters

({.+i.@>:@([:<.1{[%~{:-{.)*1{[)

I'm sure there's room for improvement here.

Usage:

   ({.+i.@>:@([:<.1{[%~{:-{.)*1{[)2 3 10
2 5 8

We can give the verb a name for an extra three characters:

   s=.({.+i.@>:@([:<.1{[%~{:-{.)*1{[)
   s 2 3 10
2 5 8

Previously:

((0-.~]*]<:2{[)(((i.100)*1{[)+{.))

Thought I'd have a go at doing this in Golfscript and got as far as .3$- 2$/1+,{2$*}%{3$+}% before realising Golfscript doesn't seem to do floats. Bugger.

Gareth

Posted 2012-06-07T00:53:50.093

Reputation: 11 678

4

bash 12:

seq $1 $2 $3

Haha - who would have thought that? Silly'o'bash which normally just handles Integer arithmetic (9/10 = 0) can handle floating points that way:

./cg-6228-arithmetic-sequences.sh 2 3 10
2
5
8
./cg-6228-arithmetic-sequences.sh 3 1.5 12
3,0
4,5
6,0
7,5
9,0
10,5
12,0

Other test-cases omitted for brevity. I have to mention that you can change the locale, if you're irritated by 10,5 which is the continental way to say 10.5. ;)

user unknown

Posted 2012-06-07T00:53:50.093

Reputation: 4 210

How very interesting! – beary605 – 2012-06-10T16:13:12.187

This could be 3 bytes as a function: seq (maybe) – CalculatorFeline – 2017-06-12T17:24:12.717

3

Ruby 34

s=->a,n,t{p a;s[a+n,n,t]if a+n<=t}

Input: the three function parameters

Output: the sequence numbers are printed, one per line

Ideone link: http://ideone.com/SahPs

Cristian Lupascu

Posted 2012-06-07T00:53:50.093

Reputation: 8 369

3

J, 26

s=:1 :']+u*[:i.@<.@>:u%~-'

There's some interesting syntax to use this definition.

   10(2 s)1
1 3 5 7 9

ephemient

Posted 2012-06-07T00:53:50.093

Reputation: 1 601

3

Ruby - 28

((t-a)/n).times{|i|p(a+i*n)}

Jon

Posted 2012-06-07T00:53:50.093

Reputation: 131

3

Mathematica, 5 bytes

Range

Arguments are a,top,n in that order. I'm assuming that decimal points at the end are okay.

CalculatorFeline

Posted 2012-06-07T00:53:50.093

Reputation: 2 608

2

Scala 52

val x,?,z=BigDecimal(readLine)
x to z by?map println

Give inputs a,n and top in the separate line. Each entry of the output will be listed in the separate line.

Prince John Wesley

Posted 2012-06-07T00:53:50.093

Reputation: 669

2

Mathematica 39 37

Table[#+#2 k,{k,0,Quotient[#3-#,#2]}]&

Usage

Table[#1 + #2 k, {k, 0, Quotient[#3 - #1, #2]}] &[2, 3, 10]
Table[#1 + #2 k, {k, 0, Quotient[#3 - #1, #2]}] &[3, 1.5, 12]
Table[#1 + #2 k, {k, 0, Quotient[#3 - #1, #2]}] &[3.14159, 2.71828, 10]
Table[#1 + #2 k, {k, 0, Quotient[#3 - #1, #2]}] &[0.1, 0.3, 2]

sequence output

DavidC

Posted 2012-06-07T00:53:50.093

Reputation: 24 524

1

JavaScript 70

s=prompt().split(",");for(n=0;(p=s[0]-0+n*s[1]-0)<=s[2]-0;n++)alert(p)

input in the form a,n,top

Griffin

Posted 2012-06-07T00:53:50.093

Reputation: 4 349

The code gives floating-point arithmetic errors. I, however, used a different test case: I will add it to the list. – beary605 – 2012-06-07T05:08:14.500

1

Perl, 37 chars

for(($_,$n,$t)=<>;$_<=$t;$_+=$n){say}

Input a, n, and top, each number on a separate line. The list is output with newlines separating the values. Run with perl -M5.010 (or perl -E).

breadbox

Posted 2012-06-07T00:53:50.093

Reputation: 6 893

1

Python (245)

Input should be given with a space in between, e.x. 0.1 0.3 0.5

a=raw_input().split()
I,F=int,float
D='.'
b=[[I(i.replace(D,'')),len(i.split(D)[1:])]for i in a[:2]]
a=map(F,a)
c=max(b,key=lambda x:x[1])[1]
for i in b:i[0]*=10**(c-i[1])
print [(b[0][0]+i*b[1][0])/10.0**c for i in range(I((a[2]-a[0])/a[1]+1))]

It avoids floating point errors by turning each floating point into a list containing two values: the number without the decimal point, and the number of significant digits. example: 1.34 -> [134, 2]

beary605

Posted 2012-06-07T00:53:50.093

Reputation: 3 904

0

Python, 37

a,n,t=input()
while a<=t:print a;a+=n

Grabs input in the form a, n, top, and prints out the numbers on separate lines.

grc

Posted 2012-06-07T00:53:50.093

Reputation: 18 565

0

C# 120 74

void A(float a,float n,int t){Console.Write(a+" ");a+=n;if(a<=t)A(a,n,t);}

Edit: New version - recursive and a lot shorter than using LINQ :)

Input: the three parameters of method A

Output: the numbers in the sequence are printed to console, separated by spaces.

Test link: http://ideone.com/o7g6k

Previous version (using LINQ):

IEnumerable<float>A(float a,float n,int t){return Enumerable.Range(0,int.MaxValue).Select(i=>a+n*i).TakeWhile(s=>s<=t);}

Cristian Lupascu

Posted 2012-06-07T00:53:50.093

Reputation: 8 369

0

PHP 83

<?function a($x,$i,$t){while($r<$t){$r=$x+$i*$g++;$s.=($r<$t?$r." ":"");}echo$s;}?>

Wish I could remove "function" but there's no shorter declaration for that in PHP. Handy that PHP uses untyped variables!

Usage: a(2,3,10); Returns: 2 5 8

TwoScoopsofPig

Posted 2012-06-07T00:53:50.093

Reputation: 131

You can save two bytes by omitting the final ?>. – breadbox – 2012-06-08T03:36:56.540

0

PHP, 53

<?for(list(,$a,$n,$x)=$argv;$a<=$x;$a+=$n)echo"$a ";

l0n3sh4rk

Posted 2012-06-07T00:53:50.093

Reputation: 1 387

0

R, 3 bytes

seq

Built in function with arguments from (a), to (top) and by (n)

usage seq(a,top,n) eg

seq(2,10,3) 

output numeric vector

2 5 8  

mnel

Posted 2012-06-07T00:53:50.093

Reputation: 826

0

TI-Basic, 20

Prompt A,N,T:For(I,A,T,N:Disp I:End

Timtech

Posted 2012-06-07T00:53:50.093

Reputation: 12 038