5
\$\begingroup\$

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!

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

19 Answers 19

5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – beary605 Jun 9 '12 at 2:44
6
\$\begingroup\$

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.

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

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.

\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ How very interesting! \$\endgroup\$ – beary605 Jun 10 '12 at 16:13
  • \$\begingroup\$ This could be 3 bytes as a function: seq (maybe) \$\endgroup\$ – CalculatorFeline Jun 12 '17 at 17:24
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Ruby - 28

((t-a)/n).times{|i|p(a+i*n)}
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 5 bytes

Range

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ The code gives floating-point arithmetic errors. I, however, used a different test case: I will add it to the list. \$\endgroup\$ – beary605 Jun 7 '12 at 5:08
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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]

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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);}
\$\endgroup\$
0
\$\begingroup\$

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

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

PHP, 53

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

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  
\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 20

Prompt A,N,T:For(I,A,T,N:Disp I:End
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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