Draft

Even more efficient last 'Fibonacci' digit in base d

Description:

Even more efficient last 'Fibonacci' digit in base d

This kata is a spinoff of Efficient last Fibonacci digit in base d, which is a spinoff of Find last Fibonacci digit [hardcore version], which is a spinoff of Find last Fibonacci digit.

Preface

The nnth Fibonacci number can be computed based on the recurrence Fn=Fn1+Fn2F_n=F_{n-1}+F_{n-2} (the first few numbers are 0,1,1,2,3,5,8,13,21,...0,1,1,2,3,5,8,13,21,...). Usually, we would have F0=0F_0=0 and F1=1F_1=1, or F0=1F_0=1 and F1=1F_1=1, but for this kata, you will be provided custom starting terms xx and yy for F0F_0 and F1F_1. Since this isn't really the Fibonacci sequence, let's call this sequence UnU_n. Based on this, this is what we have so far:

  • U0=xU_0=x
  • U1=yU_1=y
  • Un=Un1+Un2U_n=U_{n-1}+U_{n-2}

Task

Given xx, yy, nn and kk, find the last digit of UnU_n in base kk based on the above. In other words, find Un(modk)U_n\pmod k. Your function should return an integer.

Examples

If x=5x=5, y=7y=7, n=3n=3 and k=10k=10, we have the following:

U3=U2+U1U_3=U_2+U_1
U2=U1+U0U_2=U_1+U_0

U3=U1+U0+U1U_3=U_1+U_0+U_1
U3=2U1+U0U_3=2U_1+U_0
U3=x+2yU_3=x+2y
U3=1910U_3=19_{10}

This means that the last digit of U3U_3 in base 1010 is 99.


If x=4x=4, y=9y=9, n=6n=6 and k=8k=8, we have the following:

U6=U5+U4U_6=U_5+U_4
U5=U4+U3U_5=U_4+U_3
U4=U3+U2U_4=U_3+U_2
U3=U2+U1U_3=U_2+U_1
U2=U1+U0U_2=U_1+U_0

U6=U1+U0+U1+U1+U0+U1+U0+U1+U1+U0+U1+U1+U0U_6=U_1+U_0+U_1+U_1+U_0+U_1+U_0+U_1+U_1+U_0+U_1+U_1+U_0
U6=8U1+5U0U_6=8U_1+5U_0
U6=5x+8yU_6=5x+8y
U6=9210=1348U_6=92_{10}=134_8

This means that the last digit of U6U_6 in base 88 is 44.


If x=15x=15, y=15y=15, n=4n=4 and k=16k=16, we have the following:

U4=U3+U2U_4=U_3+U_2
U3=U2+U1U_3=U_2+U_1
U2=U1+U0U_2=U_1+U_0

U4=U1+U0+U1+U1+U0U_4=U_1+U_0+U_1+U_1+U_0
U4=3U1+2U0U_4=3U_1+2U_0
U4=2x+3yU_4=2x+3y
U4=7510=4b16U_4=75_{10}=4b_{16}

This means that the last digit of U4U_4 in base 1616 is bb, or 1111 in base 1010.

Constraints

  • 0n212340\leq n\leq 2^{1234}
  • 0k29870\leq k\leq 2^{987}
  • 0xy28760\leq x\leq y\leq 2^{876}

Hints (only use these if you need them)

  • Hint 1 Do you notice a pattern in the examples?
  • Hint 2 Look at the coefficients. Do you notice anything?
  • Hint 3 There must be a reason why I included the first few Fibonacci numbers in the preface.
Mathematics
Number Theory

More By Author:

Check out these other kata created by encrypted-oreo

Stats:

CreatedJan 11, 2025
Warriors Trained10
Total Skips0
Total Code Submissions17
Total Times Completed4
Python Completions4
Total Stars1
% of votes with a positive feedback rating50% of 1
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments1
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • encrypted-oreo Avatar
Ad