formula for knowing the fibonacci character at certain index - math

Is there any formula you can directly apply for knowing a fibonacci character within a word without having to construct the word from scratch.
For example. Let's consider:
0 a
1 b
2 ba
3 bab
4 babba
5 babbabab
Is there a way to find what character is in w(4) at index 3 which in this case is b as
w(4) equals babba provided you know beforehand that w(4) has 5 characters?
Thanks

That would have been really easy to google.
Note: there are no words, like w(4), they are just construction steps of the single infinite word.
You can find the closed formula and its description on wikipedia.

Related

What is the correct name for those mathematical operations?

I am not an english speaker, however I need to write code where I need to include print messages in English, hence using english terminology from Math, statistics etc.
This is the case:
I have two lists and I compare them, let's say:
list 1 - 1 2 3 4 5
list 2 - 2 4 6
So naturally when I compare both lists you see that 2 4 are present in both lists. What is the operation itself called? Because when I try to translate it from my language to english it's "section" or "cutting". I don't believe that this is the official mathematical term for this operation.
Also I want to know what is it called when you show the things that are missing in both lists. For example 1 3 5 6 ?
Thanks and sorry for the silly question.
Intersection for {1,2,3,4,5} ; {2,4,6} = {2,4}
Symmetric difference for {1,2,3,4,5} ; {2,4,6} = {1,3,5,6}

R: gmp: inaccurate output from mod.bigz

I downloaded the gmp package in order to calculate the modular exponentiation of very large numbers. But one of its functions, mod.bigz, seems to fail beyond a certain number of digits. For example, the answer to 100...00 mod 3 should be 1 since 99...99 is divisible by 3. But the answer I get is sometimes 0 or 2. Is there any way to fix this or is gmp just not accurate for very large numbers?
https://cran.r-project.org/web/packages/gmp/index.html
#install.packages('gmp')
library(gmp)
mod.bigz(100000000000000000000000000000000000000000000000000,3)
# 2
mod.bigz(10000000000000000000000000000000000000000000000000000000,3)
# 0
I think my overall advice is avoid falling back to base R at any point when you have those numbers in your code. If you fall back on regular R (or "regular most any programming language") at some point then it breaks.
For the original example you could wrap the inner number in pow.bigz:
mod.bigz(pow.bigz(10,50), 3)
# 1
mod.bigz(pow.bigz(10,55),3)
# 1
For the more complicated example we discussed in the comments, i.e. 693487563928456923569873549873658638579865348726988458, we get to the real solution which is to avoid falling back on R for the number via the character class:
mod.bigz("693487563928456923569873549873658638579865348726988458",3) # should be 0
# 0
mod.bigz("100000000000000000000000000000000000000000000000000",3) # should be 1
# 1

Program asked in a online hiring challenge

Given N integers in the form of Ai where 1≤i≤N, the goal is to find the M that minimizes the sum of |M-Ai| and then report that sum.
For example,
Sample Input: 1 2 4 5
Sample Output: 6
Explanation: One of the best M′s you could choose in this case is 3.
So the answer = |1−3|+|2−3|+|4−3|+|5−3| = 6.
The approach I used is sort the given input and take the middle number as M.
But I was not able to solve all the test cases. I am unable to find any other approach for this question. Where did I go wrong?(Please help me this question has been bugging me from the past 2 days.Thanks)
Can M be any real number or must it be an integer?
If there are no constraints on M your algorithm must work fine.
If M must be an integer then you have to choose M among floor(The Middle Number) and ceiling(The Middle Number).
In which language did you code up the algorithm?

Arranging Vigenere Cipher into columns

As I understand if you arrange a Vigenere cipher into columns you can use the Index Of Coincidence to find out the key length.
I'm struggling to write an Algorithm that would take a piece of text and arrange it into columns.
For example -
1 2 3 4 5 6 7 8 9 10
Would return this if the period is 2 -
1,3,5,7,9
2,4,6,8,10
and perform an IOC test on each of these strings
IF the period is 3 -
1,4,7,10
2,5,8
3,6,9
and perform an IOC test on each of these strings
Etc etc.
I've constructed an IOC test however I'm struggling to think of an algorithm to split the text up into collumns, any tips on how to think more like a computer scientist and construct algorithms like this?
If you already know the key length, it's pretty trivial. If you don't know the key length, you have to guess it by entropy. Here is an example in Python for instance:
if you_dont_know_key_length:
key_length = find_key_length_by_entropy(ciphertext)
columns = [ciphertext[i::key_length] for i in xrange(key_length)]
Any language should basically have the same construct (pick every n-th element in the ciphertext)

Index Vectors with Factors in R

I have a factor RFyhat which I'm looking to convert to a numeric vector. I've already discovered that
as.numeric(levels(RFyhat))[RFyhat]
works as desired, and I've played around a bit with this construction:
c(1,2,20,4,5,6,7)[RFyhat]
also works as expected (RFyhat has 7 levels).
So I understand the behavior of this construction, but I'm wondering if anyone can explain how this syntax is intended to work, or whether it is just 'syntactic sugar'. More specifically, does [RFyhat] act as an index vector? If it does, how do factors generally behave when used as an index?
Yes, I believe that factors gets converted to integers when used for indexing, rather than characters or anything else.
Look at this example
> fac <- factor(letters[c(1,1,2,1,3,3,2,1)])
> vec <- c(b=1, a=2, c=3)
> vec[fac]
b b a b c c a b
1 1 2 1 3 3 2 1
So element 1 of fac has returned element 1 of vec, regardless of the different order of names.
Personally I'd prefer as.integer(as.character(RFyhat)) to as.numeric(levels(RFyhat))[...].

Resources