5
\$\begingroup\$

This question already has an answer here:

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.

\$\endgroup\$

marked as duplicate by Mr. Xcoder, Community Sep 7 '17 at 17:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ A useless sequence that's not on OEIS..? \$\endgroup\$ – Stewie Griffin Sep 7 '17 at 17:28
  • \$\begingroup\$ @StewieGriffin Exactly. \$\endgroup\$ – Mr. Xcoder Sep 7 '17 at 17:28
  • 3
    \$\begingroup\$ On 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. \$\endgroup\$ – Conor O'Brien Sep 7 '17 at 17:49
  • \$\begingroup\$ I'd agree with you (though I also have a hammer) \$\endgroup\$ – DJMcMayhem Sep 7 '17 at 17:50
  • 2
    \$\begingroup\$ Rotating the sum's digits left has a period of 59. \$\endgroup\$ – Jonathan Allan Sep 7 '17 at 17:54
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 72 bytes \$\endgroup\$ – Halvard Hummel Sep 7 '17 at 17:39
  • 4
    \$\begingroup\$ Hello and welcome to PPCG! Great first answer! Also, mind if I ask whether you have anything to do with this user? \$\endgroup\$ – Mr. Xcoder Sep 7 '17 at 17:43
  • \$\begingroup\$ @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. \$\endgroup\$ – user70408 Oct 29 '17 at 20:08
  • \$\begingroup\$ @VoteToReopen No, that is not a problem. I was just asking out of cheer curiosity :) \$\endgroup\$ – Mr. Xcoder Oct 29 '17 at 20:09
3
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ This is a repost of my answer on the old deleted question. \$\endgroup\$ – Erik the Outgolfer Sep 7 '17 at 17:12
  • \$\begingroup\$ If you take input from STDIN, you can eliminate both 0. \$\endgroup\$ – Dennis Sep 7 '17 at 17:33
  • \$\begingroup\$ @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. \$\endgroup\$ – Erik the Outgolfer Sep 7 '17 at 17:34
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ -1 byte: change ~~ to + \$\endgroup\$ – Conor O'Brien Sep 7 '17 at 17:47
  • \$\begingroup\$ Oh, yeah. Thanks, @ConorO'Brien; the ~~ snuck back in there when I rewrote this on my phone. \$\endgroup\$ – Shaggy Sep 7 '17 at 18:02
1
\$\begingroup\$

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.

\$\endgroup\$

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