22
\$\begingroup\$

Write a program or named function that will output or return the sequence up to the nth integer in the Iccanobif sequence, documented on OEIS as A014258. Note that only the zeroth element in the sequence (0) will be printed if n is zero.

The sequence is generated by starting like the standard Fibonacci sequence, but after adding the two previous numbers, you flip the result and drop any leading zeros. An interesting fact, to me at least, is that this sequence is not strictly increasing (see the list below). It also seems to be (and probably is) strictly greater than or equal to the Fibonacci sequence.

Your program's input must be an integer.

The first 20 numbers of the sequence are provided here for your viewing pleasure:

0, 1, 1, 2, 3, 5, 8, 31, 93, 421, 415, 638, 3501, 9314, 51821, 53116, 739401, 715297, 8964541, 8389769

Standard loopholes are forbidden.

Shortest program wins.

EDIT: Added a note to clarify that the sequence starts with the zeroth element and should be included if n is zero.

Example IO possibilities:

0    ->    0
1    ->    0 1
6    ->    0 1 1 2 3 5 8
17   ->    [0, 1, 1, 2, 3, 5, 8, 31, 93, 421, 415, 638, 3501, 9314, 51821, 53116, 739401, 715297]

Now that there are several answers, below are my implementations in Python 2 that I worked hard to hide with markup:

Iterative:

#Closest to my initial program. 73 bytes. It should also be noted that this program
 cannot reach a stack overflow. It runs for n=5000 in less than 10 seconds.

i,a,b=input(),0,1
print a
while i:print b;i,a,b=i-1,b,int(str(a+b)[::-1])

Recursive:

#Note that this prints n trailing newlines. 64 bytes.
 Will hit a stack overflow error for large values of n.

def f(n,i=0,j=1):print i,n and f(n-1,j,int(str(i+j)[::-1]))or'';

\$\endgroup\$
  • 8
    \$\begingroup\$ +1 for thinking of something to do with the Fibonacci sequence that hasn´t been done before \$\endgroup\$ – Level River St Jun 23 '15 at 21:38
  • \$\begingroup\$ @steveverrill I decided I wanted to make another challenge, then started with just deciding to see what the sequence would look like, after I imagined it. So I wrote a program. Then I searched OEIS and made the challenge. \$\endgroup\$ – mbomb007 Jun 23 '15 at 21:42
  • \$\begingroup\$ Was it inspired by this question? \$\endgroup\$ – JohnE Jun 23 '15 at 23:58
  • \$\begingroup\$ @JohnE No. I've seen it before, but that challenge is one of the most simple challenges on this site. No, I was just creating a number sequence purely from my imagination that I could use as a challenge. \$\endgroup\$ – mbomb007 Jun 24 '15 at 1:15
  • 3
    \$\begingroup\$ I think you should wait a little longer before accepting an answer. Unless one of the answers is clearly unbeatable (e.g., a 1 byte solution), it's advisable to wait at least a week. \$\endgroup\$ – Dennis Jun 24 '15 at 6:13

18 Answers 18

3
\$\begingroup\$

Pyth, 17 15 14

Pu+Gs_`s>2GQU2

Try it online

Very basic implementation, starts with range(2) and adds a number of elements equal to the input, then slices off the extras pops off the last element.

Thanks @Jakube for pointing out the > reversal thingy.

Explanation

Pu+Gs_`s>2GQU2    : Q = eval(input) (implicit)
P                 : all but the last element
 u         QU2    : reduce Q times starting with [0, 1]
  +G              : add to the previous result
       s>2G       : sum of the last two elements of G
    s_`           : int(reversed(repr(above value)))
\$\endgroup\$
4
\$\begingroup\$

Python 2, 58 bytes

a=0;b=1
exec"print a;a,b=b,int(str(a+b)[::-1]);"*-~input()

Uses str to convert rather than backticks because large enough numbers in Python 2 are written with an L at the end. I tried a recursive function, but it turned out longer (61):

f=lambda n,a=0,b=1:-~n*[0]and[a]+f(n-1,b,int(str(a+b)[::-1]))
\$\endgroup\$
3
\$\begingroup\$

Julia, 79 bytes

f=n->(t=[0,1];for i=2:n push!(t,t[i]+t[i-1]|>string|>reverse|>int)end;t[1:n+1])

This creates a function that accepts an integer as input and returns an integer array.

Ungolfed + explanation:

function f(n)
    # Start with the usual Fibonacci stuff
    t = [0,1]

    # Looooooooooooooop
    for i = 2:n
        # Compute the Iccanobif number by piping results
        iccanobif = t[i] + t[i-1] |> string |> reverse |> int

        # Jam it into t
        push!(t, iccanobif)
    end

    # Return the first n + 1
    t[1:n+1]
end

Examples:

julia> f(1)
2-element Array{Int64,1}:
 0
 1

julia> f(17)
18-element Array{Int64,1}:
      0
      1
      1
      2
      3
      5
      8
     31
     93
    421
    415
    638
   3501
   9314
  51821
  53116
 739401
 715297
\$\endgroup\$
3
\$\begingroup\$

T-SQL, 149

Very straight forward inline table function that uses a recursive CTE query. As it is using INTs this will top out at 37. Adding in CASTs for bigints will allow it to go further to 63

create function i(@ int)returns table return with r as(select 0I,1N union all select n,reverse(i+n)+0from r)select 0n union all select top(@)n from r

It is used as follows

select * from i(0)
n
-----------
0

(1 row(s) affected)

select * from i(1)
n
-----------
0
1

(2 row(s) affected)

select * from i(6)
n
-----------
0
1
1
2
3
5
8

(7 row(s) affected)

select * from i(17)
n
-----------
0
1
1
2
3
5
8
31
93
415
421
638
3501
9314
51821
53116
715297
739401

(18 row(s) affected)
\$\endgroup\$
3
\$\begingroup\$

K, 25 23 bytes

{-1_ x{x,.|$+/-2#x}/!2}

A simple modification of one of the examples at No Stinking Loops.

The phrase .|$ casts a number to a string, reverses it and then evaluates it.

Edit:

Sloppy attention to boundary conditions on my part. More correct now:

  {-1_ x{x,.|$+/-2#x}/!2}'0 1 6 10
(,0
 0 1
 0 1 1 2 3 5 8
 0 1 1 2 3 5 8 31 93 421 415)

Edit 2:

(x+1)# can be replaced with -1_, saving 2 characters. The space is necessary because otherwise _x would be an identifier, when I want the "drop" operator applied to a variable called x.

\$\endgroup\$
  • 2
    \$\begingroup\$ According to the OP, the output should start with a zero. \$\endgroup\$ – Stretch Maniac Jun 23 '15 at 21:42
  • \$\begingroup\$ Correct- Should be fixed now. \$\endgroup\$ – JohnE Jun 23 '15 at 21:46
  • 1
    \$\begingroup\$ Came here to post an answer only to see you had the exact same one. Nicely done. \$\endgroup\$ – tmartin Jun 24 '15 at 15:20
3
\$\begingroup\$

Haskell, 64 49 bytes

a!b=a:b!(read$reverse$show$a+b)
q n=0:take n(1!1)

Usage example: q 15 -> [0,1,1,2,3,5,8,31,93,421,415,638,3501,9314,51821,53116]

How it works: ! recursively builds an infinite list of iccanobif numbers starting with it's first argument (the second argument must be the next iccanobif number). q takes the first n numbers from the iccanobif list starting with 1, 1 and prepends a 0.

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

CJam, 18 bytes

U1{_2$+sW%i}ri*;]p

How it works

U1                      e# First two numbers in the series
  {        }ri*         e# Run the loop input numbers times
   _2$+                 e# Get sum of last two numbers in the series
       sW%i             e# Convert to string, inverse and convert back to a number
                ;       e# Remove the last number to get only first n + 1 numbers.
                 ]p     e# Wrap all numbers in an array and print the array

Try it online here

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

Java - 126 124

I haven't seen Java around this site in awhile...

void f(int b){for(int c=0,d=1,g;b-->=0;d=Integer.valueOf(new StringBuilder(c+(c=d)+"").reverse()+""))System.out.println(c);}

f(5) prints 0 1 1 2 3 5 8 31 93 421 415 638

\$\endgroup\$
  • \$\begingroup\$ I would also accept ...System.out.println(c); \$\endgroup\$ – mbomb007 Jun 23 '15 at 21:46
  • \$\begingroup\$ @mbomb007 Thanks! Saved me 2 bytes. \$\endgroup\$ – Stretch Maniac Jun 23 '15 at 21:47
  • \$\begingroup\$ You could probably shorten it using the numeric method to reverse a number since Java's string manipulation is costly. \$\endgroup\$ – mbomb007 Jun 24 '15 at 1:18
  • \$\begingroup\$ I know it's been more than 1.5 years, but you can save 6 bytes by replacing Integer.valueOf( with new Long( (and then change the int in the for-loop to long as well). If you prefer to just work with integers instead, new Integer( is still shorter than Integer.valueOf(. \$\endgroup\$ – Kevin Cruijssen Feb 2 '17 at 8:18
2
\$\begingroup\$

SWI-Prolog, 141 131 121 bytes

a(X,R):-X>1,A is X-1,a(A,B),reverse(B,[K,L|_]),W is K+L,name(W,Z),reverse(Z,Y),name(E,Y),nth0(X,R,E,B);X=1,R=[0,1];R=[0].

Running a(17,X). outputs:

[0, 1, 1, 2, 3, 5, 8, 31, 93, 421, 415, 638, 3501, 9314, 51821, 53116, 739401, 715297] 

Takes about 10 seconds to output the result of a(10000,X). on my computer.

Edit: The 121 bytes version above is a one predicate definition = one liner. The old 131 bytes version is the following (must be runned as p(17,X)):

a(0,[0]).
a(1,[1,0]).
a(X,[E|B]):-A is X-1,a(A,B),B=[K,L|_],W is K+L,name(W,Z),reverse(Z,Y),name(E,Y).
p(X,Y):-a(X,Z),reverse(Z,Y).
\$\endgroup\$
2
\$\begingroup\$

><> (Fish) 592 254 Bytes

Not super golfed (42/43 blanks that do nothing and a total of 30 redirection tokens) , but was an interesting exercise getting it working in the first place.

10!/{:}0=?v/{1-}}:{+:0}!/a,:1%-:0=?!v~{:1!/$:@0=?!v$~}}:&{{&*\
/-$/    ;n/\oo", "n:    \       }+1{/     \$-1$*a /|.!20}}01@/
* :{:}(?v:{!":}-1!/$:@0=?!v$~{:}1!/$:@0=?!v$~}}}:&{{{&*:1%-*&{{&+}}{1+}02.
b .1 +bb   \      \$-1$*a /       \$-1$,a /
\*9{~~~{/

You can test it here, providing the desired length in the initial stack.

EDIT: More than halved byte count

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

PHP, 114, 109 bytes

function f($n){if($n==0)return 0;$f=[0,1];for($i=2;$i<=$n;++$i){$f[$i]=strrev($f[$i-1]+$f[$i-2]);}return $f;}

Nothing fancy, just an average fibonacci algorithm with the string reverse magic.

Ungolfed:

function f($n)
{
    if($n == 0) return 0;
    $f = [0, 1];
    for ($i=2; $i<=$n; ++$i){
        $f[$i] = strrev($f[$i-1] + $f[$i-2]);
    }
    return $f;
}
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 279 bytes

n = InputBox("n")
For i = 0 To n
If i < 2 Then
Cells(i + 1, 1) = i
ElseIf i > 6 Then
x = Cells(i, 1) + Cells(i - 1, 1)
l = Len(x)
v = CStr(x)
For j = 1 To l
r = r + Right(v, 1)
v = Left(v, l - j)
Next j
Cells(i + 1, 1) = r
r = ""
Else
Cells(i + 1, 1) = Cells(i, 1) + Cells(i - 1, 1)
End If
Next i

Running the macro will prompt the user to enter a value for n.

The results will then be printed row by row in column A:

Output

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you remove spaces in your code to make it shorter? \$\endgroup\$ – mbomb007 Jun 24 '15 at 14:19
  • \$\begingroup\$ @mbomb007 when writing in Excel VBA the spaces are automatically entered, so I just leave them in. \$\endgroup\$ – Wightboy Jun 24 '15 at 14:49
1
\$\begingroup\$

JavaScript (ES2015), 81 73 bytes

(a,b=0,c=1)=>{for(;a-->-1;c=[...(b+(b=+c)+"")].reverse().join``)alert(b)}

Running this function (named f) with 6:

f(6);// alerts: 0, 1, 1, 2, 3, 5, 8
\$\endgroup\$
1
\$\begingroup\$

Pip, 13 bytes

I'm pretty sure all the features used in this program were present in Pip before this question was asked.

LaSio:+RVi+oi

Takes input as command-line argument. Try it online!

Explanation

               a is 1st cmdline arg; i is 0; o is 1 (implicit)
La             Loop (a) times:
       RVi+o   Reverse of i+o
      +        Unary + treats its operand as a number, thus removing leading 0's
    o:         Assign the result to o...
  Si           ... before swapping i and o
            i  After the loop, output i

The two variables' values evolve like so:

Iter   o   i (output)
   0   1   0
   1   0   1
   2   1   1
   3   1   2
   4   2   3
   5   3   5
   6   5   8
   7   8  31
   8  31  93
   9  93 421
  10 421 415
\$\endgroup\$
0
\$\begingroup\$

Pushy, 18 bytes (non-competing)

Z1{:2d+vFs@KjkvF;_

Try it online!

It's not the most elegant of programs, but it works.

Z1     \ Push 0 and 1 to begin the sequence
{:     \ Input times do:
 2d+   \   Add the last two terms
 vF    \   Send to second stack
 s     \   Split into digits
 @Kjk  \   Reverse and join into one number
 vF;    \   Send back to first stack
_      \ At the end of the program, print the whole stack.
\$\endgroup\$
  • \$\begingroup\$ @mbomb007 yep, sorry! \$\endgroup\$ – FlipTack Feb 2 '17 at 19:50
0
\$\begingroup\$

Jelly, 9 bytes (non-competing)

0+ṚV$¥Ð¡1

Try it online!

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

R, 134 bytes

i=function(n){s=c(0,1);for(i in 3:n){s[i]=as.numeric(paste0(rev(strsplit(as.character(s[i-2]+s[i-1]),'')[[1]]),collapse=''))};cat(s)}

Example:

> i(10)
0 1 1 2 3 5 8 31 93 421

Would love to see if somebody had a better R alternative than to take your number, make it a string, reverse it and turn it back into a number again.

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

Groovy, 70 bytes

{r={"$it".reverse() as int};f={n->n<3?1:r(f(n-1))+r(f(n-2))};r(f(it))}

{
    r={"$it".reverse() as int};       // Reverse digits, costly using string.
    f={n->n<3?1:r(f(n-1))+r(f(n-2))}; // Recursive Iccanobbif implementation.
    r(f(it))                          // Reverse final output.
}
\$\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.