Question

If anyone can help understand how i solve this Batmanacci The Fibonacci sequence can be defined...

If anyone can help understand how i solve this

Batmanacci

The Fibonacci sequence can be defined as follows:

fib1fib2fibn=1=1=fibn−2+fibn−1fib1=1fib2=1fibn=fibn−2+fibn−1

We get the sequence 1,1,2,3,5,8,13,21,…1,1,2,3,5,8,13,21,…. But there are many generalizations of the Fibonacci sequence. One of them is to start with other numbers, like:

f1f2fn=5=4=fn−2+fn−1f1=5f2=4fn=fn−2+fn−1

And we get the sequence 5,4,9,13,22,35,57,…5,4,9,13,22,35,57,…. But what if we start with something other than numbers? Let us define the Batmanacci sequence in the following manner:

s1s2sn=N=A=sn−2+sn−1s1=Ns2=Asn=sn−2+sn−1

where ++ is string concatenation. Now we get the sequence N, A, NA, ANA, NAANA, ….

Given NN and KK, what is the KK-th letter in the NN-th string in the Batmanacci sequence?

Input

Input consists of a single line containing two integers NN (1≤N≤1051≤N≤105) and KK (1≤K≤10181≤K≤1018). It is guaranteed that KK is at most the length of the NN-th string in the Batmanacci sequence.

Output

Output the KK-th letter in the NN-th string in the Batmanacci sequence.

Sample Input 1 Sample Output 1
7 7
N
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

from collections import defaultdict

fibs = defaultdict(lambda: -1)
fibs[1] = 1
fibs[2] = 1


def length(n):
if n > 87:
return -1
if n not in fibs:
fibs[n] = length(n - 2) + length(n-1)
return fibs[n]


def bat(n, k):
if n == 1:
return 'N'
if n == 2:
return 'A'
l = length(n-2)
while l == -1:
n -= 1
l = length(n-2)
if l < k:
return bat(n-1, k-l)
return bat(n-2, k)


a, b = tuple(map(int, input().split()))
print(bat(a, b))

Code Screenshot:-

Output Screenshots:-

Note: Could you please consider my effort on this work and give up vote. Thank you :)

Add a comment
Know the answer?
Add Answer to:
If anyone can help understand how i solve this Batmanacci The Fibonacci sequence can be defined...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • • Fibonacci numbers, denoted as Fn, form a sequence, called the Fibonacci sequence, such that each...

    • Fibonacci numbers, denoted as Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. • Fn = Fn-1 + Fn-2 (n > 1) • Fo = 0 and F1 = 1 • Submit the R script to write the first 100 numbers of Fibonacci sequence, i.e., F, to F99, to a file named as Fibonacci_100.txt and put in the folder in path /user/R/output/. •...

  • MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo...

    MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo = 0 and F1 = 1. Hence F2 = 1, F3 = 2, F4 = 3, etc. In this problem you will use three different methods to compute the n-th element of the sequence. Then, you will compare the time complexity of these methods. (a) Write a recursive function called fibRec with the following declaration line begin code function nElem = fibrec (n) end...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • 1. The famous Fibonacci sequence f1, f2, f3, . . . is defined as f1 =...

    1. The famous Fibonacci sequence f1, f2, f3, . . . is defined as f1 = 1, f2 = 1 fn = fn−1 + fn−2, for n > 2 So the sequence begins as 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .. Define a recursive function int fibonacci(int n) which returns the n-th Fibonacci number 2. Define recursive function my_sequence(n) which returns the n-th member of the sequence a1 = 3, a2 = 5, a3 =...

  • 3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F...

    3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F for all n E N and with Fi = F2= 1. to find a recursive relation for the sequence of ratios (a) Use the recursive relation for (F) Fn+ Fn an Hint: Divide by Fn+1 N (b) Show by induction that an 1 for all n (c) Given that the limit l = lim,0 an exists (so you do not need to prove that...

  • turn the following if function in to C++ % Define variables: % fn -- Fibonacci number...

    turn the following if function in to C++ % Define variables: % fn -- Fibonacci number % n -- The item in the sequence to calculate % Get n n = input('Enter the Fibonacci number n to evaluate (n>2): '); % Check to see that n is an integer greater than two if n <= 2 disp('Error--n must greater than two!'); elseif round(n) ~= n disp('Error--n must be an integer!'); else % Calculate fn fn = zeros(1,n); fn(1) = 1;...

  • Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 =...

    Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1, where i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. Write a recursive function definition in C++ that has one parameter n of type int and that returns the n-th Fibonacci number. You can call this function inside the main function to print the Fibonacci numbers. Sample Input...

  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • (5) Fibonacci sequences in groups. The Fibonacci numbers F, are defined recursively by Fo = 0,...

    (5) Fibonacci sequences in groups. The Fibonacci numbers F, are defined recursively by Fo = 0, Fi-1, and Fn Fn-1 + Fn-2 for n > 2. The definition of this sequence only depends on a binary operation. Since every group comes with a binary operation, we can define Fibonacc type sequences in any group. Let G be a group, and define the sequence (n in G as follows: Let ao, ai be elements of G, and define fo-ao fa and...

  • In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in...

    In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in which each succeeding number is the sum of the previous two.     0, 1, 1, 2, 3, 5, 8, 13, 21, …. Practice your knowledge of recursion by producing a program that prints the nth Fibonacci number. That is, the program should accept an integer (n) as input and output the number that is the nth number in the Fibonacci sequence. For example, if n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT