Output the Iccanobif Sequence

22

2

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'';

mbomb007

Posted 2015-06-23T20:33:30.900

Reputation: 21 944

8+1 for thinking of something to do with the Fibonacci sequence that hasn´t been done before – Level River St – 2015-06-23T21:38:55.690

@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. – mbomb007 – 2015-06-23T21:42:55.167

Was it inspired by this question?

– JohnE – 2015-06-23T23:58:20.733

@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. – mbomb007 – 2015-06-24T01:15:54.483

3I 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. – Dennis – 2015-06-24T06:13:29.073

@Dennis I was going to update which answer is accepted if it gets beaten, but I'll un-accept it. You'll probably have to remind me to accept an answer, unless PCG sends reminders. – mbomb007 – 2015-06-24T15:48:43.013

@mbomb007 I usually accept an answer after a day or so, and update it when it's beaten. It's not a big deal either way. – undergroundmonorail – 2015-06-25T05:59:11.130

@mbomb007 Why do you require a named function? – flawr – 2017-02-02T09:58:11.733

Idk. I wrote it in 2015. I'm not going to change it now. – mbomb007 – 2017-02-02T14:24:32.357

Answers

3

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

FryAmTheEggman

Posted 2015-06-23T20:33:30.900

Reputation: 16 206

4

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

xnor

Posted 2015-06-23T20:33:30.900

Reputation: 115 687

3

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.

JohnE

Posted 2015-06-23T20:33:30.900

Reputation: 4 632

2According to the OP, the output should start with a zero. – Stretch Maniac – 2015-06-23T21:42:18.840

Correct- Should be fixed now. – JohnE – 2015-06-23T21:46:00.473

1Came here to post an answer only to see you had the exact same one. Nicely done. – tmartin – 2015-06-24T15:20:14.240

3

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

Alex A.

Posted 2015-06-23T20:33:30.900

Reputation: 23 761

3

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.

nimi

Posted 2015-06-23T20:33:30.900

Reputation: 34 639

3

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)

MickyT

Posted 2015-06-23T20:33:30.900

Reputation: 11 735

2

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

Alvaro Arregui

Posted 2015-06-23T20:33:30.900

Reputation: 131

2

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

Optimizer

Posted 2015-06-23T20:33:30.900

Reputation: 25 836

2

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

Stretch Maniac

Posted 2015-06-23T20:33:30.900

Reputation: 3 971

I would also accept ...System.out.println(c); – mbomb007 – 2015-06-23T21:46:08.783

@mbomb007 Thanks! Saved me 2 bytes. – Stretch Maniac – 2015-06-23T21:47:58.593

You could probably shorten it using the numeric method to reverse a number since Java's string manipulation is costly.

– mbomb007 – 2015-06-24T01:18:09.060

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(. – Kevin Cruijssen – 2017-02-02T08:18:37.160

2

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

Fatalize

Posted 2015-06-23T20:33:30.900

Reputation: 32 976

2

><> (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

Fongoid

Posted 2015-06-23T20:33:30.900

Reputation: 971

1

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

DLosc

Posted 2015-06-23T20:33:30.900

Reputation: 21 213

1

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

Downgoat

Posted 2015-06-23T20:33:30.900

Reputation: 27 116

1

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

Wightboy

Posted 2015-06-23T20:33:30.900

Reputation: 339

1Can you remove spaces in your code to make it shorter? – mbomb007 – 2015-06-24T14:19:35.517

@mbomb007 when writing in Excel VBA the spaces are automatically entered, so I just leave them in. – Wightboy – 2015-06-24T14:49:15.317

0

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.

FlipTack

Posted 2015-06-23T20:33:30.900

Reputation: 13 242

@mbomb007 yep, sorry! – FlipTack – 2017-02-02T19:50:00.733

0

Jelly, 9 bytes (non-competing)

0+ṚV$¥Ð¡1

Try it online!

Erik the Outgolfer

Posted 2015-06-23T20:33:30.900

Reputation: 38 134

0

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.

Andrew Haynes

Posted 2015-06-23T20:33:30.900

Reputation: 311

0

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

Magic Octopus Urn

Posted 2015-06-23T20:33:30.900

Reputation: 19 422