What is general name of described math function - algorithm

I need to know the name of this function, please help.
The idea of this function is that you have some number and you need to finding two biggest numbers from fibonacci sequence which you need to add to get this number. And method returns to you array of this fibonacci digits but the numbers in this array represented by 0 and 1 where two numbers 1 and all others 0. And the position of 1 in array is the same where the biggest numbers as in fibonacci seq.
For example i have fibonacci sequence {1,1,2,3,5,8,13}
Number = 11 so the two numbers from sequence will be 8 and 3
The output will be {0,0,0,1,0,1,0}
Its pretty famous as i remember and I'm not completeley sure but using of fibonacci is one of the solutions of it. Please help to get the name of it so i could find out more about it

This is not function, but numeral system: Fibonacci coding - representation of integer as sum of Fib. numbers

Related

How to use the Pisano Period to find the Last Digit of the Sum of Fibonacci Numbers?

I'm taking an online algorithms course, and I've come across a problem where I need to find the last digit of the sum of the Fibonacci numbers up the nth (current) number.
I need some help connecting the dots. As I understand it, the "Last Digit of the Sum of Fibonacci Numbers" problem has a solution that is somehow related to the Pisano Period.
But I don't really understand what that means.
The Pisano Period was used to calculate the remainder given some value of m for an extremely large Fibonacci Number, which was the focus of a prior problem (I.E., Solve Fn mod m = ???).
Forum posts (and the instruction set) seem to suggest that the length can somehow help us quickly zero in on the sum for the current Fibonacci without having to actually build up to it normally through a loop.
I would rather avoid just looking at someone else's solution if possible, so if anyone has any useful hints that can help me see the missing link, I would really appreciate it.
The last digit of a Fibonacci number is just that number reduced modulo 10. Pisaso periods are the periods of which the sequence of fibonacci numbers, modulo some base, repeat. So, if you're interested in F(x) mod 10, you'd interested in the Pisaso Period p(10).
If we have this period, say it was something like [1, 5, 2, 7, 0] (its not, but for sake of example), we'd know that the 3rd integer in the sequence ended with a "2". And because it repeats, we'd know the 8th integer also ends in a "2", and the 13th...
Generalizing this, we could say that the last digit of the number N could be found at the ith index in our list we just built, for i satisfying N = 5 * k + i, where k is just any integer, and 5 comes from the fact that our list has 5 elements (and thus repeats every 5 values). Rewriting this, we could say i = N mod 5.
Putting that all together (spoilers), we just need to find the actual values of the repeating sequence mod 10, and then take our input value N (for finding the Nth Fibonacci number mod 10), and index into said repeatingSequence at index N mod len(repeatingSequence) for our answer.
For reference, for base 10, the actual repeating sequence is:
011235831459437077415617853819099875279651673033695493257291

Divide binary array into three parts such that each part represent same decimal

Given a binary array, divide array into three parts such that each part represent same decimal.
Eg arr[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1}
Above array can be divided in the following way:
{1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1}. Now each part represent same decimal.
One simple approach would be to iterate starting from 1 and check each decimal can divide the array into three equal parts.
Is there any efficient algorithm for this.
Count the number of set bits. There's no solution if this number isn't divisible by three. Say it is, and that there are 3k set bits. Find the positions of the first, k+1, and 2k+1 set bits. Keep incrementing all three positions and comparing until you get to the last set bit or get a disagreement. If you get to the last set bit with all three agreeing then you have a solution.

Maximum number of equal elements in array

I was solving the problems from codeforces practice problem achieve.
I am not able to find efficient solution.
How to solve the following problem?
I can only think of a brute force solution
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
he chooses two elements of the array ai, aj (i ≠ j);
he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
Output
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
Sample test(s)
input
2
2 1
output
1
input
3
1 4 1
output
3
find the sum of all the elements.
If the sum%n==0 then n else n-1
EDIT: Explanations :
First of all it is very easy to spot that the answer is minimum n-1.It cannot be lesser .
Proof: Choose any number that you wish to make as your target.And suppose the last index n.Now you make a1=target by applying operation on a1 and an.Similarly on a2 and an and so on.So all numbers except the last one are equal to target.
Now we need to see that if sum%n==0 then all numbers are possible.Clearly you can choose your target as the mean of all the numbers here.You can apply operation by choosing a index with value less than mean and other with value greater than mean and make one of them (possibly both) equal to mean.

Finding numbers whose digits sum to a prime

I was trying to solve this problem on SPOJ, in which I have to find how many numbers are there in a range whose digits sum up to a prime. This range can be very big, (upper bound of 10^8 is given). The naive solution timed out, I just looped over the entire range and checked the required condition. I cant seem find a pattern or a formula too. Could someone please give a direction to proceed in??
Thanks in advance...
Here are some tips:
try to write a function that finds how many numbers in a given range have a given sum of the digits. Easiest way to implement this is to write a function that returns the number of numbers with a given sum of digits up to a given value a(call this f(sum,a)) and then the number of such numbers in the range a to b will be f(sum,b) - f(sum, a - 1)
Pay attention that the sum of the digits itself will not be too high - up to 8 * 9 < 100 so the number of prime sums to check is really small
Hope this helps.
I (seriously) doubt whether this 'opposite' approach will be any faster than #izomorphius's suggestion, but it might prompt some thoughts about improving the performance of your program:
1) Get the list of primes in the range 2..71 (you can omit 1 and 72 from any consideration since neither is prime).
2) Enumerate the integer partitions of each of the prime numbers in the list. Here's some Python code. You'd want to modify this so as not to generate partitions which were invalid, such as those containing numbers larger than 9.
3) For each of those partitions, pad out with 0s to make a set of 8 digits, then enumerate all the permutations of the padded set.
Now you have the list of numbers you require.
Generate the primes using the sieve of Eratosthenes up to the maximum sum (9 + 9...). Put them in a Hash table. Then you could likely loop quickly through 10^8 numbers and add up their sums. There might be more efficient methods, but this should be quick enough.

Design a divide and conquer algorithm that can determine the missing integer

Given an array, A, containing, in some order, all but one of the k-bit non-negative integers find the missing integer with the following constraint:
You are allowed to do only two things with one of these integers:
Obtain the value of its ith bit
Swap them with one another in the array
For example: if k is 3 the array will contain all but one the integers 0 through 7 inclusive, the task is to find the missing integer.
You should check that each bit (for example, if k = 3 you need to check that all 3 bits) are present 2^k-1 times as 0 & 1.
If you are sure of the invariant of the list that you outlined (only one number is missing and there are no duplicates) - sure that it always holds - then you could simply check only one, arbitrary bit holds the condition outlined above.
For example, say that the invariant holds, k = 3, and you have the list [0-7], excluding 6.
For each number in the list, get its first bit (least significant) and do the following:
if bit value = 0 then zeroValues++;
if bit value = 1 then oneValues++;
zeroValues should equal oneValues and both should equal 2^k-1, in this case 4.
Edit: Rereading your question, you're looking for the entire number. In order to find the whole integer value that you are looking for, just do the procedure for all bits. For each bit you do it, you will find either a 0 value or 1 value missing. The missing value is the bit value in the result integer. Doing this for all bits will find the entire bit representation of your integer.
Hint: Can you find out if the missing number is even or odd?
For a hint for a totally different approach, can you sort the array?

Resources