Recursive Fibonacci algorithm with variant - python-2.7

I've implemented a traditional fibonacci recursion related with the reproduction of a population of rabbits in python2.7
def fibonacci(n):
if n is 0 or n is 1: return 1
else: return (fibonacci(n-1)+fibonacci(n-2))
This code computes the population if exactly one month after two rabbits mate, they produce one male and one female rabbit. Now I need to modify the code to compute the population if every pair of reproduction-age rabbits produces a litter of k rabbit pairs (instead of only 1 pair). How can I do this in a recursive way? What is the most appropriate model for the recursive case?

Well let's try and derive the sequence, which is also known as a multi-nacci sequence of order k. See this paper for some more interesting tidbits.
The basic rule is the same: it takes 1 month to for a pair to mature
Therefore at:
Month 0: 1 pair exists (young)
Month 1: 1 pair exists (adult)
Month 2: 1 pair exists (adult) + k pairs exist (young) = 1 + k total
Month 3: 1 + k pairs exist (adult) + k pairs exist (young) = 1 + 2k total
Month 4: 1 + 2k pairs exist (adult) + k (1 + k) pairs exist (young) = 1 + k(3 + k) total
Month 5: 1 + 3k + k^2 pairs exist (adult) + k (1 + 2k) pairs exist (young) = 1 + k(4 + 3k) total
and so on...
Notice that with each month, the total number of rabbit pairs is k * number of adults (which are two generations old) + number of young pairs (which are 1 generation old). Therefore the amount of rabbits at generation n is k * number of rabbits at generation n - 2 (as these are now all adults) + number of rabbits at generation n - 1
Mathematically: f(0) = 1, f(1) = 1, f(n) = f(n-1) + k * f(n-2).
Code:
def fibnum(n, k):
if n < 0:
raise ValueError("n must be a positive value")
if n is 0 or n is 1:
return 1
else:
return (fibnum(n-1, k) + k * fibnum(n-2, k))
Notes:
This code is 0-based: so 5th month is n=4 and so on.
WARNING: this sequence grows ridiculously fast with larger k (so calculating fibnum(100, 100) may take a while). Memoization is advised to provide a significant speedup for increasing n.
Update: Changed code to accept k as a parameter, and cleaned up my code formatting a bit. Added reference to multinacci sequence

Related

Find minimum prime numbers which sum to a given value

I want to find the minimum set of prime numbers which would sum to a given value e.g. 9 = 7 + 2 (not 3+3+3).
I have already generated a array of prime numbers using sieve of eratosthens
I am traversing the array in descending order to get the array largest prime number smaller than or equal to given number. This works great if the number is odd.
But fails for even numbers e.g 122 = 113 + 7 + 2 but 122 = 109 +13.
From Golbach's Conjecture we know that any even number can be represented as two sum of two prime numbers. So if a number is even we can directly return 2 as output.
But I am trying to figure out a way other than brute force to find minimum prime numbers.
Although your question didn't say so, I assume you are looking for the set of primes that has the smallest cardinality.
If n is even, then consider the primes p in order, 2, 3, 5, …; eventually n - p will be prime, so n is the sum of two primes. This process typically converges very quickly, with the smaller of the two primes seldom larger than 1000 (and usually much smaller than that).
If n is odd, and n - 2 is prime, then n is the sum of the primes 2 and n - 2.
If n is odd, and n - 2 is not prime, then n - 3 is even and can be written as the sum of two primes, as described above.
Thus you can always find two or three primes that sum to any target n greater than 3.
Try this out!
Not an ideal code but if you want to have a working solution :P
primes = [2,3,5,7]
D = 29
i = -1
sum_ = 0
count = 0
while sum_ != D :
sum_ = sum_ + primes[i]
count += 1
if (sum_ == D) :
break
elif D - sum_ == primes[i-1] :
count += 1
break
elif D - sum_ < ex[i-1] and (D-sum_ not in primes) :
sum_ = sum_ - primes[i]
count = count - 1
i = i - 1
print(count)

Dynamic Programming solution for a Recursion solution

Given an input n , find the sum of all the possible combinations of numbers 1 ... n.
For example, if n=3 , then all the possible combinations are
(1),(2),(3),(1,2),(1,3),(2,3),(1,2,3)
and their sum is
1 + 2 + 3 + (1+2) + (1+3) + (2+3) + (1+2+3) =24
I am able to solve this problem using recursion. How can I solve this problem using Dynamic Programming ?
#include<iostream>
using namespace std;
int sum=0,n;
int f(int pos,int s)
{
if(pos>n)
{
return 0;
}
else
{
for(int i=pos+1;i<=n;++i)
{
sum+=s+i;
f(i,s+i);
}
}
}
int main()
{
cin>>n;
sum=0;
f(0,0);
cout<<sum<<'\n';
}
}
EDIT
Though this problem can be solved in constant time using this series.
But I want to know how this can be done using Dynamic Programming as I am very weak at it.
You do not need to use dynamic programming; you can use simple arithmetic if you want.
The number of cases is 2 ^ n, since each number is either on or off for a given sum.
Each number from 1 to n is used in exactly half of the sums, so each number comes 2 ^ (n-1) times.
1 + 2 + ... + n = (n - 1) * n / 2.
So the sum is (n - 1) * n / 2 * 2 ^ (n-1).
For n = 3, it is (4*3/2) * 4 = 24.
EDIT: if you really want to use dynamic programming, here's one way.
Dynamic programming makes use of saving the results of sub-problems to make the super problem faster to solve. In this question, the sub-problem would be the sum of all combinations from 1 ... n-1.
So create a mapping from n -> (number of combinations, sum of combinations).
Initialize with 1 -> (2,1). Because there are two combinations {0,1} and the sum is 1. Including 0 just makes the math a bit easier.
Then your iteration step is to use the mapping.
Let's say (n-1) -> (k,s), meaning there are k sets that sum to s for 1 ... n-1.
Then the number of sets for n is k * 2 (each combination either has n or does not).
And the sum of all combinations is s + (s + k * n), since you have the previous sum (where n is missing) plus the sum of all the combinations with n (which should be k * n more than s because there are k new combinations with n in each).
So add n -> (2*k,2*s + k*n).
And your final answer is the s in n -> (k,s).
let dp[n] be the result, Therefore:
dp[1] = 1
dp[n] = 2 * dp[n-1] + 2^(n-1) * n
First, it is obvious that dp[1] = 1
Second, dp[n] is the sum which contains n and sum which didn't contains n
E.G: dp[3] = {(1) (2) (1,2)} + {(3), (1,3), (2,3), (1,2,3)}
We can find dp[n-1] appear twice and the number of n appear 2^(n-1) times
I think maybe it is what you want.

Recursive solution for Permutations

Hey I have a problem where I need to create two functions, countWithPerms() and ignorePerms() These two functions must be a recursive solution. countWithPerms() will count the number of actual permutations while ignorePerms() will only count the number of duplicate permutations.
So an example would be find the permutation for the number 3. So if I pass 3 into the function countWithPerms() would find that 3 = (2 + 1) = (1 + 2) = (1 + 1 + 1), so countWithPerms(3) is 3, because it counted 3 ways to sum up 3. While countIgnorePerms(3) is 2 because (1 + 2) and (2 + 1), would both not be counted in countWithPerms since they are the just written in the opposite order.
A large example would be countWithPerms(7) is 63, while countIgnorePerms(7) is 14.
I have countwithPerms done, but I am completely stuck on countIgnorePerms.
int countWithPerms( int n)
{
if(n == 1)
return 0;
else
n--;
return (countWithPerms(n) + 1) +
(countWithPerms(n));
}
int ignorePerms(int sum, int xmin){
if(sum == 1)
return 0;
else
for(int i=0; i<sum;i++){
sum += sum-xmin;
2*ignorePerms(sum,xmin)+1;
return sum;
}
}
The idea of counting without considering permutations is to consider only ordered solutions.
To do this pass in addition to n also what is the minimum value xmin that an addendum must have. For example
3 = 1 + 2
would be ok (because 2 >= 1), but
3 = 2 + 1
wouldn't be acceptable (because 1 < 2).
So the idea is to write a function that answers "how many sums with non-decreasing terms can give the prescribed total in the first addendum is not less than min_addendum?".
if min_addendum is bigger than total clearly the answer is 0
if total is 1 then there's only one sum
otherwise the first possible sum is total, then you should count as sums
min_addendum + a sum of other non-decreasing terms, the first not less than min_addendum totalling total-min_addendum
min_addendum+1 + a sum of other non-decreasing terms, the first not less than min_addendum+1 totalling total-min_addendum-1
min_addendum+2 + a sum of other non-decreasing terms, the first not less than min_addendum+2 totalling total-min_addendum-2
...

Number of Increasing Subsequences of length k

I am trying to understand the algorithm that gives me the number of increasing subsequences of length K in an array in time O(nklog(n)). I know how to solve this very same problem using the O(k*n^2) algorithm. I have looked up and found out this solution uses BIT (Fenwick Tree) and DP. I have also found some code, but I have not been able to understand it.
Here are some links I've visited that have been helpful.
Here in SO
Topcoder forum
Random webpage
I would really appreciate if some can help me out understand this algorithm.
I am reproducing my algorithm from here, where its logic is explained:
dp[i, j] = same as before num[i] = how many subsequences that end with i (element, not index this time)
have a certain length
for i = 1 to n do dp[i, 1] = 1
for p = 2 to k do // for each length this time num = {0}
for i = 2 to n do
// note: dp[1, p > 1] = 0
// how many that end with the previous element
// have length p - 1
num[ array[i - 1] ] += dp[i - 1, p - 1] *1*
// append the current element to all those smaller than it
// that end an increasing subsequence of length p - 1,
// creating an increasing subsequence of length p
for j = 1 to array[i] - 1 do *2*
dp[i, p] += num[j]
You can optimize *1* and *2* by using segment trees or binary indexed trees. These will be used to efficiently process the following operations on the num array:
Given (x, v) add v to num[x] (relevant for *1*);
Given x, find the sum num[1] + num[2] + ... + num[x] (relevant for *2*).
These are trivial problems for both data structures.
Note: This will have complexity O(n*k*log S), where S is the upper bound on the values in your array. This may or may not be good enough. To make it O(n*k*log n), you need to normalize the values of your array prior to running the above algorithm. Normalization means converting all of your array values into values lower than or equal to n. So this:
5235 223 1000 40 40
Becomes:
4 2 3 1 1
This can be accomplished with a sort (keep the original indexes).

Number of calls for nth Fibonacci number

Consider the following code snippet:
int fib(int N)
{
if(N<2) return 1;
return (fib(N-1) + fib(N-2));
}
Given that fib is called from main with N as 10,35,67,... (say), how many total calls
are made to fib?
Is there any relation for this problem?
PS: This is a theoretical question and not supposed to be executed.
EDIT:
I am aware of other methods for the faster computation of Fibonacci series.
I want a solution for computing number of times fib is invoked for fib(40),fib(50) ,.. without the aid of compiler and in exam condition where you are supposed to answer 40 question similar to this one in a stipulated of time ( about 30 mints).
Thanks,
Let f(n) be the number of calls made to calculate fib(n).
If n < 2 then f(n) = 1.
Otherwise, f(n) = 1 + f(n - 1) + f(n - 2).
So, f is at least O(fib(n)). In fact, f(n) is 2 * fib(n) - 1. We show this by induction:
Base cases (n < 2, that is, n = 0 or n = 1):
f(n) = 1 = 2 * 1 - 1 = 2 * fib(n) - 1.
Induction step (n >= 2):
f(n + 1) = f(n) + f(n - 1) + 1
f(n + 1) = 2 * fib(n) - 1 + 2 * fib(n - 1) - 1 + 1
f(n + 1) = 2 * fib(n + 1) - 1
There exist efficient ways to calculate any Fibonacci term. Thus the same holds for f(n).
Is there any relation for this problem
?
There is a close-form equation for the nth fibonacci number: http://en.wikipedia.org/wiki/Fibonacci_number#Closed_form_expression
In the pseudocode you posted, the number of calls satisfies the recurrence relation
x(n) = x(n-1) + x(n-2) +1 # for n>=2
x(1) = 1
x(0) = 1
This is almost same as the Fibonacci recurrence relation. Proof by induction can show that the number of calls to fib made by fib(n) is equal to 2*fib(n)-1, for n>=0.
Of course, the calculation can be sped up by using the closed form expression, or by adding code to memorize previously computed values.
As mentioned above, you need to solve the following recurring equation:
K(n)=K(n-1)+K(n-2)+1
Let's write it for n-1: K(n-1)=K(n-2)+K(n-3)+1
Now, subtract the second one from the first one:
K(n)-K(n-1) = K(n-1) - K(n-3),
or
K(n) - 2*K(n-1) + K(n-3) = 0.
The respective characteristic equation will be:
x^3 - 2*x^2 + 1 = 0.
It has the following roots: 1, (1+sqrt(5))/2, (1-sqrt(5))/2
Thus for any real A,B,C the following function
K(n) = A*(1)^n + B*((1+sqrt(5))/2)^n + C*((1-sqrt(5))/2)^n
will be a solution for your equation.
To find A,B,C you need to define several initial values K(0), K(1), K(2) and solve the system of equations.
phi is a constant
position = ceil(log((n - 0.5)*sqrt(5))/log(phi));
n is the fibonacci number...
position will give you the which fibonacci number is n
for example given 13 , position will be 7 - 0 1 1 2 3 5 8 13
using this position just calculate the fibonacci number at position-1 or any position you want relative to given fibonacci number.
Previous Fibo Num = floor((pow(phi,position-1)/sqrt(5))+0.5);
floor((pow(phi, position)/sqrt(5))+0.5) - is the standard formula for calculating Nth fibonacci num (Note - This is not an approximation)
I have just reverse this formula to calculate the position and use the position - 1 to calculate the previous fibonacci number.
Ref - http://itpian.com/Coding/20951-Given-the-Nth-fib-no-and-find-the--N-1-th-fib-number-without-calculating-from-the-beginning---------.aspx
This is a classic problem for solving with Recurrence Relations.
Specifically, the fibonacci problem has the following parameters:
f(0) = 1
f(1) = 1
f(n) = f(n-1) + f(n-2)
Once you master solving recurrences, you'll have no problem reaching the solution (which, incidently, is exactly the same as fib(n)).
Interesting question, I can't give you a formula, but I wrote a Ruby program to do it, it works on numbers I figured out on paper, and it should work for any.
#!/usr/bin/ruby
#find out how many times fib() would need to be called
def howmany(n)
a = [ ]
a.push n-1
a.push n-2
while a.select{|n| n > 2}.length > 0
a.map! do |n|
n > 2 ? [n-1,n-2] : n
end
a.flatten!
end
a.length
end
.
>> howmany(10)
=> 55
It's slow.. I'm figuring out 35 right now, I'll edit when it finishes.
Edit:
>> howmany(35)
=> 9227465