Implement the Fibonacci sequence... Shifted to the right

5

1

Background

We all know the famous Fibonacci sequence, where each term is the sum of the previous two, with the first term being 0 and the second one being 1. The first few elements are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765

What we are going to implement today is a similar sequence, starting at 0 and 1 as well, and each term is the sum of the previous two, but the sum's digits are shifted by one place to the right. What does "shifted" mean? – A digit shifted to the right is basically that digit + 1, unless it equals 9, which maps to 0 instead. Just as a reference, here is the full list of mappings:

0 -> 1;  1 -> 2;  2 -> 3;  3 -> 4;  4 -> 5;  5 -> 6;  6 -> 7;  7 -> 8;  8 -> 9;  9 -> 0

With all that being said, these are the first terms of our Fibonacci-shifted sequence:

0, 1, 2, 4, 7, 22, 30, 63, 4, 78, 93, 282, 486, 879, 2476, 4466, 7053, 22620, 30784, 64515

Given an integer N (non-negative if 0-indexed and positive if 1-indexed), finds the Nth term of the Fibonacci-shifted sequence.

Rules

  • The output may contain leading zero(es), in case 9 is the first (few) digit(s).

  • Input and output can be handled using any default mean.

  • Take note that Loopholes are forbidden.

  • This is , hence your program will be scored in bytes, with less bytes being better.

Mr. Xcoder

Posted 2017-09-07T17:08:51.277

Reputation: 39 774

Question was closed 2017-09-07T17:52:26.600

1A useless sequence that's not on OEIS..? – Stewie Griffin – 2017-09-07T17:28:28.990

@StewieGriffin Exactly. – Mr. Xcoder – 2017-09-07T17:28:43.150

3On reflection, this should be a duplicate of Fibonacci... it's literally just digit + 1 mod 10 for each digit after the sum is performed. I have a hammer so I'm not voting, yet. – Conor O'Brien – 2017-09-07T17:49:21.620

I'd agree with you (though I also have a hammer) – James – 2017-09-07T17:50:46.707

@ConorO'Brien I'd agree too...also have hammer. – Erik the Outgolfer – 2017-09-07T17:52:01.210

@ConorO'Brien "Hammered" it myself. – Mr. Xcoder – 2017-09-07T17:52:39.100

2Rotating the sum's digits left has a period of 59. – Jonathan Allan – 2017-09-07T17:54:16.373

Answers

4

Python 2, 78 72 bytes

f=lambda n:n>1and int(''.join(`-~int(i)%10`for i in`f(n-1)+f(n-2)`))or n

Try it online!

Saved 6 bytes thanks to Halvard Hummel.

user70408

Posted 2017-09-07T17:08:51.277

Reputation:

72 bytes – Halvard Hummel – 2017-09-07T17:39:16.613

4

Hello and welcome to PPCG! Great first answer! Also, mind if I ask whether you have anything to do with this user?

– Mr. Xcoder – 2017-09-07T17:43:14.043

@Mr.Xcoder I don't know them, but my name was inspired by theirs. Is that a problem? EDIT: Never mind, might as well change my name. – None – 2017-10-29T20:08:16.060

@VoteToReopen No, that is not a problem. I was just asking out of cheer curiosity :) – Mr. Xcoder – 2017-10-29T20:09:41.353

3

Jelly, 9 bytes

ð+D‘%⁵Ḍø¡

Try it online!

-2 thanks to Dennis.

Explanation:

ð+D‘%⁵Ḍø¡ Full program. Takes input through STDIN.
ð         Start new dyadic chain
 +        Add the left (f(n - 1)) and right (f(n - 2)) arguments
  D       Get list of decimal digits
   ‘      Increment each
    %⁵    Modulo ⁵ (10)
      Ḍ   Convert from decimal to integer
       ø  Start new niladic chain
        ¡ Repeat previous chain z times (where z is taken from STDIN as there are no arguments)

Erik the Outgolfer

Posted 2017-09-07T17:08:51.277

Reputation: 38 134

This is a repost of my answer on the old deleted question. – Erik the Outgolfer – 2017-09-07T17:12:21.170

If you take input from STDIN, you can eliminate both 0. – Dennis – 2017-09-07T17:33:57.597

@Dennis I was expecting Jelly-related action of yours after seeing your comment :p EDIT: There was a comment on the question that now got deleted. – Erik the Outgolfer – 2017-09-07T17:34:38.257

3

JavaScript (ES6), 63 62 bytes

This is what I'd come up with before the challenge was deleted, don't have time to golf it further now.

Returns the nth number in the sequence, 0-indexed.

f=(n,x=0,y=1)=>n?f(--n,y,+[...x+y+""].map(z=>++z%10).join``):x
  • 1 byte saved thanks to Conor.

Try it online


Alternative, 55 bytes

A quick port of VoteToReopen's excellent Python solution.

f=n=>n>1?+[...f(--n)+f(--n)+""].map(x=>++x%10).join``:n

Try it online

Shaggy

Posted 2017-09-07T17:08:51.277

Reputation: 24 623

-1 byte: change ~~ to + – Conor O'Brien – 2017-09-07T17:47:06.503

Oh, yeah. Thanks, @ConorO'Brien; the ~~ snuck back in there when I rewrote this on my phone. – Shaggy – 2017-09-07T18:02:52.677

1

J, 40 bytes

[`(-&2(10|>:)&.(10&#.inv)@+&$:<:)@.(1&<)

Try it online!

Highlighted following is the standard Fibonacci husk:

[`(-&2                    +&$:<:)@.(1&<)

Then, we compose the following function with the result of adding the previous two terms:

      (10|>:)&.(10&#.inv)@

This, in turn, is the function 10|>: atop 10&#.inv, which gets the digits of a number. That is to say, the digits are obtained, then the function 10|>: is applied, then the digits are combined to make a number again. 10|>: is a fork that is equivalent to (y + 1) % 10 in a conventional language for input y.

Conor O'Brien

Posted 2017-09-07T17:08:51.277

Reputation: 36 228