I have an array: A B AB BAB ABBAB BABABBAB ... The number of each term of the array is base on the Fibonacci number.Put the n-th string and the n+1-th string together, then producing the n+2-th string,eg.BABABBAB = BAB + ABBAB. Then is the 10^16-th letter is A or B?
相关问题
- how to split a list into a given number of sub-lis
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
- Convert Array to custom object list c#
- pick a random item from a javascript array
相关文章
- JSP String formatting Truncate
- Numpy matrix of coordinates
- What are the problems associated to Best First Sea
- Selecting only the first few characters in a strin
- Coin change DP solution to keep track of coins
- PHP: Can an array have an array as a key in a key-
- Python: print in two columns
- Algorithm for partially filling a polygonal mesh
Let
f[n]
be then
th fibonacci number.Assume we want to find the value at position
x
in the string obtained by concatenatingf[1], f[2], ..., f[n]
.k
such thatf[1] + f[2] + ... + f[k] >= x
. So positionx
belongs to word that hasf[k]
characters (at least in the concatenation it does. But since all words are made up froma
andb
, we'll try to reduce the problem to just those two).x
in the termf[k]
, subtractf[1] + ... + f[k - 1]
fromx
.k = 1
printa
, ifk = 2
printb
, else go to step 4.f[k - 2] < x
, then the position we're looking for is in the word corresponding to (with length)f[k - 1]
. Subtract 1 fromk
andf[k - 2]
fromx
and go to step 3.f[k - 2]
. Subtract 2 fromk
, leavex
unchanged and go to step 3.This doesn't require generating the actual words, just their lengths, which are the basic fibonacci numbers.
Worked example - note that I'm only using the actual words for illustration purposes, they are not needed.
Concatenating all these we get:
ababbababbabbababbab
. Let's ask ourselves what's at position10
(it'sb
).please don't take this answer too serious:
i never was good and math and this sounds like this task should be too heavy to calculate without a freaky algorithm, so my solution would be to simply have a guess. to choose between
A
andB
, i would write a very simple programm like this in php to print out the first 15 or 20 "lines":the result shows there are always less
A
s (38%) thanB
s (62%) - so the chance to be right when guessingB
aren't that bad.