# Arithmetic Sequences

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!

• 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 Jun 7 '12 at 5:11
• Function/standalone is fine. There is no specified input or output: the programmer will state both. – beary605 Jun 7 '12 at 5:49
• What 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 Jun 7 '12 at 9:33
• I changed "floating points" to "decimals". – beary605 Jun 7 '12 at 15:53
• The input/output part should be specified in the question, not just in the comments. – user unknown Jun 8 '12 at 13:41

## 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.

• 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 Jun 9 '12 at 2:44

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.

• 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 Jun 7 '12 at 20:17
• @leftaroundabout If it was my problem, I'd accept that output! (But it's not.) – breadbox Jun 8 '12 at 3:32

## J, 3433 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.

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. ;) • How very interesting! – beary605 Jun 10 '12 at 16:13 • This could be 3 bytes as a function: seq (maybe) – CalculatorFeline Jun 12 '17 at 17:24 ### 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 ## 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  # Ruby - 28 ((t-a)/n).times{|i|p(a+i*n)}  # Mathematica, 5 bytes Range  Arguments are a,top,n in that order. I'm assuming that decimal points at the end are okay. ### 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. # 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] # JavaScript 70 s=prompt().split(",");for(n=0;(p=s-0+n*s-0)<=s-0;n++)alert(p)  input in the form a,n,top • The code gives floating-point arithmetic errors. I, however, used a different test case: I will add it to the list. – beary605 Jun 7 '12 at 5:08 ## 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).

# 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)
for i in b:i*=10**(c-i)
print [(b+i*b)/10.0**c for i in range(I((a-a)/a+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]

## 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.

### 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.

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);}


# 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

• You can save two bytes by omitting the final ?>. – breadbox Jun 8 '12 at 3:36

## PHP, 53

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


# 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


# TI-Basic, 20

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