Finding sum of fibonacci numbers recursively - algorithm

I'm a bit stuck here. I know a particular fibonacci number can be found recursively as so:
int fib (int n)
{
if (n <= 1)
return n;
else
return fib(n-1) + fib(n-2);
}
And I know iteratively I could call that function n times to find the sum of fibonacci numbers
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += fib(i);
}
But I'm having a hard time coming up with a recursive function to find the sum. I don't think it would be much different than the original fibonacci function. (This is for an assignment aimed at improving my ability to write ocaml syntax, not writing recursive functions)

Since no one else is bothering to answer your question, here you go:
int fib_sum(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return fib_sum(n-1) + fib_sum(n-2) + 1;
}

If you want a recursive solution involving only fib_sum(), here is one:
int fib_sum (int n)
{
if (n == 0)
return 1;
if (n == 1)
return 2;
return fib_sum(n-1) + fib_sum(n - 2) + 1;
}

Observing that fib_sum(n) == fib(n+2) - 1 you can use more or less the same function.

I guess this will work fine
int fib_sum (int n){
if(n<=1) {
return n;
}
else {
return fib_Sum(n-1) + fib_Sum(n-2) + 1;
}
}

Keep in mind that counting of elements starts from 0
so elements 0 1 1 2 3 5 8 13
then position 0 1 2 3 4 5 6 7
So if n = 5, the sum = (0+1+1+2+3+5)=12
recursive function will be:
int fib_sum (int n){ if(n<=1) { return n; } else { return fib_Sum(n-1) + fib_Sum(n-2) + 1; } }

Related

Codility PermMissingElem

My solution scored only 40% correctness on Codility.
What am I doing wrong?
Here is the test result (https://codility.com/demo/results/trainingU7KSSG-YNX/)
Problem:
A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Solution:
function solution(A) {
var output = 1;
var arrayLength = A.length;
if(!arrayLength){
return output;
}
if(arrayLength == 1) {
return A[0] + 1;
}
var sorted = A.sort(sortingFn);
for(var i = 0; i < A.length - 1; i++) {
if(A[i+1] - A[i] > 1) {
output = A[i] + 1;
break;
}
}
return output;
}
function sortingFn(a, b) {
return a - b;
}
Result
Your algorithm find the missing element by comparing neighboring elements in the array. This means it is incapable of handling cases where the first or last element is missing, as these only have a single neighbor.
Consider as an example [1, 2, 3]. The missing element would be 4. But since 4 has precisely one neighbor (3), it can't be found by the algorithm, and 1 will be returned.
In addition your algorithm is rather inefficient, as sorting takes O(n lg n), while the problem is solveable in O(n):
find_missing(arr):
s = sum(arr)
s' = (len(arr) + 1) * (len(arr) + 2) / 2
return s' - s
This code works by summing up all elements in the array and comparing it to expected sum, if all elements were present. The advantage of this approach is that it only requires linear operations and will find the missing element with relative simplicity.
Try this in c#:
using System;
using System.Linq;
private static int PermMissingElem(int[] A)
{
if (!A.Any() || !A.Any(x => x == 1)) { return 1; }
var size = A.Length;
var numberTwoList = Enumerable.Range(1, size);
var failNumber = numberTwoList.Except(A);
if (!failNumber.Any()) { return A.Max() + 1; }
return failNumber.FirstOrDefault();
}
Well, when the last element is missing, you obviously return 1, since your if statement's condition is always false. Same for first element.
Take as example this input:
1 2 3 4 5
the difference will be always 1, but element 6 is missing.
The reason for this incapability of your algorithm to catch these cases, is that it examines neighboring elements (A[i + 1] and A[i]).
JS solution #1
function solution(A) {
if (A.length === 1) {
return A[0] > 1 ? 1 : 2;
}
const map = {};
let max = 0;
for (let i = 0, len = A.length; i < len; i++) {
map[A[i]] = A[i];
if (A[i] > max) {
max = A[i]
}
}
for (let i = 0, len = A.length; i < len; i++) {
if (!map[i + 1]) {
return i + 1;
}
}
return max + 1
}
JS solution #2
function solution(A) {
const s = A.reduce((a, b) => {return a + b}, 0);
const s2 = (A.length + 1) * (A.length + 2) / 2
return s2 - s;
}
try this arrays (like the end test):
[1,2,3] -> must return 4;
[1] -> must return 2;
[2] -> must return 1;
[2,3] -> must return 1;
[1, 3] -> 2
But for #2 solution [4] returns -1 and for [123] returns -120. The test will show 100 points. But actually, it doesn't work as expected on my opinion.
Both solutions work with the same performance.
Try this javascript function:
function solution(A) {
let givenSum = 0;
let expectedSum = 0;
let size = A.length;
for(let i = 1; i <= size +1; i++){
expectedSum = expectedSum + i;
}
for(let i = 0; i < size; i++){
givenSum += A[i];
}
return expectedSum - givenSum;
}
here is my solution:
https://app.codility.com/demo/results/trainingMZWVVT-55Y/
function solution(A) {
A = A.sort((a,b)=>a-b)
if(A[0]!==1) return 1
for(let i = 0; i < A.length; i++)
{
if(A[i+1]-A[i]!==1) return A[i] + 1
}
return A[A.length] + 1
}
Tested on Codility with 100% score see here
The solution i implemented is using set difference. since the question guaranties exactly one element is missing.
def solution(A):
# write your code in Python 3.6
N = len(A)
difference = set(range(1, N+2)) - set(A)
return difference.pop()

Run time Complexity

I believe that the following code is big theta of n^3, is this correct?
for (int i = 0; i < n; i ++)
{ // A is an array of integers
if (A[i] == 0) {
for (int j = 0; j <= i; j++) {
if (A[i] == 0) {
for (int k = 0; k <= j; k++) {
A[i] = 1;
}
}
}
}
}
And that the following is big theta of nlog(n)
for (int i = 1; i < n; i *= 2)
{
func(i);
}
void func(int x) {
if (x <= 1) return;
func(x-1);
}
because the for loop would run log(n) times, and func runs at most n recursive calls.
Thanks for the help!
Your intuition looks correct. Note that for the first bit if the input contains non-zero elements the time complexity drops down to big-theta(n). If you remove the checks it would definitely be big-theta(n^3).
You are correct about the second snippet, however the first is not Big-Theta(n^3). It is not even O(n^3)! The key observation is: for each i, the innermost loop will execute at most once.
Obviously, the worst-case is when the array contains only zeros. However, A[i] will be set to 1 in the first pass of the inner-most loop, and all subsequent checks of if (A[i] == 0) for the same i will be evaluated to false and the innermost loop will not be executed anymore until i increments. Therefore, there are total of 1 + 2 + 3 + .. + n = n * (n + 1) / 2 iterations, so the time complexity of the first snippet is O(n^2).
Hope this helps!

looking for algorithm to calculate h-index fast

http://en.wikipedia.org/wiki/H-index
this wiki page is a definition of h-index
basically if I were to have an array of [ 0 3 4 7 8 9 10 ], my h-index would be 4 since I have 4 numbers bigger than 4. My h-index would've been 5 if I were to have 5 numbers bigger than 5, and etc. Given an array of integers bigger or equal to 0, what are the ways of calculating h-index efficiently?
edit: the array is not necessarily sorted
Here my realization O(N) with tabling, this is simple and blazing fast:
private static int GetHIndex(int[] m)
{
int[] s = new int[m.Length + 1];
for (int i = 0; i < m.Length; i++) s[Math.Min(m.Length, m[i])]++;
int sum = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
sum += s[i];
if (sum >= i)
return i;
}
return 0;
}
This could be done in O(n) time.
Find median of the array.
if median > (n-1)/2 then the number comes before median. Find it iteratively
If median < (n-1)/2 then the number comes after median. Find it iteratively.
If median == (n-1)/2 then the median is the solution
Here I am assuming that n is odd. Change algorithm slightly for even n (replace (n+1)/2 with n/2 assuming rank of median is n/2). Also, finding actual median in O(n) time is complicated. Use a good pivot instead (as in quicksort).
Complexity: n+n/2 +n/4... = O(n)
Answer in c# but easily convertable to java as well
public int HIndex(int[] citations) {
Array.Sort(citations);
var currentCount = 0;
var length = citations.Length;
for (var i = citations.Length - 1; i >= 0; i--)
{
currentCount = length - i;
// if the count of items to the right is larger than current value it means thats the max we can expect for hindex
if (currentCount - 1 >= citations[i])
{
return currentCount - 1;
}
}
return currentCount;
}
This is one solution I could think of. not sure if its the best.
Sort the array in ascending order. complexity nlog(n)
Iterate through the array from the index 0 to n. complexity of n
and for each iteration, suppose index is i
if (arr[i] == (arr.length - (i+1))
return arr[i]
e.g.,
arr =[ 0 3 4 7 8 9 10 ]
arr[2] = 4
i = 2
arr.length = 7
4 = (7- (2+1))
This is in O(nlogn) time but sort and concise.
public static int hindex(int[] array) {
Arrays.sort(array);
int pos = 0;
while (pos < array.length && array[pos] <= array.length - pos) {
pos++;
}
return array[pos - 1];
}
n=size of array
sort the array
then h-index = max(min(f(i),i) for i=1:n)
since h-index can never exceed n, replace all numbers in array greater
than n with n.
Now use count sort to sort the array.
time complexity O(n)
space complexity O(n)
I was not happy with my previous implementation, so I replaced it with a faster solution written in Java.
public int hIndex(int[] citations) {
if(citations == null || citations.length == 0)
{
return 0;
}
Arrays.sort(citations);
int hIndex = 0;
for(int i=0;i<citations.length;i++)
{
int hNew;
if(citations[i]<citations.length-i)
{
hNew = citations[i];
if(hNew>hIndex)
{
hIndex = hNew;
}
}
else if(citations[i]>=citations.length-i)
{
hNew = citations.length-i;
if(hNew>hIndex)
{
hIndex = hNew;
}
break;
}
}
return hIndex;
}

method that returns the sum of the odd integers from 1 to n (inclusive)?

This is what I have got but it doesn't work. When I try to compile I get this error message:
int result = 0;
^^^^^^^^^^^^^^^
Unreachable code
My code:
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int result = 0;
for(int i = n - 1; i > 0; i--)
{
if(i % 2 != 0) {
result = result + i;
}
}
return result;
}
if(n < 1);
return 0;
is equivalent to :
if(n < 1) {
}
return 0;
It shoud be replaced by :
if(n < 1)
return 0;
or (the right way)
if(n < 1) {
return 0;
}
The statement:
if(n < 1);
Is a no op because of the semi-colon. The comparison is evaluated, and nothing is done, whatever the result of the comparison is.
Then, the next line is executed, which returns 0.
if(n < 1); is your problem. The rest of the code is unreachable beacuse the following return' is always exectued.
Remove the ; after if(n < 1).
As others have said, the semi colon on your if statement is the problem. Personally however I would just do it like this:
public int sumOfOddIntegers (int n)
{
int result = 0;
if(n < 1)
return result;
for(int i = 1; i <= n; i += 2)
{
result += i;
}
return result;
}
This way you can halve the number of iterations. We know every other number is odd, so why even bother iterating the even ones and checking if they're odd when we know they're not?
the sequence is a arithmetic progression with common difference of 2.
so its sum would be given by formula :
sum = n/2(2a+(n-1)d
where n = Math.ceil(k); where k is the given number.
and d = 2, a=1
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int totalNumber = Math.ceil(n/2);
return (totalNumber/2)*(2 + (totalNumber-1)*2);
`

How to represent Big O(n!) Time complexity from for loop?

For Example
O(n)
for (int i=0;i<n;i++)
After Edit : My Final Answer is
for(int i =(n - 1); i > 1; i--)
{
factorial = factorial * i;
}
for (int j=n-2;j<factorial;j++)
{
}
The simplest answer is
for (int i = 0; i < Factorial(n); i++) {...
In practice, usually O(n!) algorithms are those that work by trying all the different permutations of a list, that is, all the different ways you can reorder a list. One example is finding the shortest line that passes through all points in a map called the travelling salesman problem. You need to try all the different ways to go through all the points and that would be O(n!).
IEnumerable<List<int>> nextPermutation(List<int> nodesLeft)
{
if (nodesLeft.Count == 0)
{
yield return new List<int>();
}
else
{
for (int i = 0; i < nodesLeft.Count; i++)
{
List<int> newNodesLeft = new List<int>(nodesLeft);
newNodesLeft.removeAt(i);
foreach (List<int> subPermutation in nextPermutation(newNodesLeft)
{
subPermutation.add(nodesLeft[i]);
yield return subPermutation;
}
}
}
}
void main()
{
foreach (List<int> permutation in nextPermutation(new List<int>(new int[]{1,2,3,4,5}))) {
//every permutation of [1,2,3,4,5] will be generated here
//this will take O(n!) to complete, where n is the number of nodes given (5 in this case)
}
}
If recursion is allowed then:
void loop(int n)
{
if(n == 1)
return; // the program gets here exactly n! times
for(int i=0; i<n; i++)
loop(n-1);
}
If we're on the same page here... I think that would look like..
Tau(fetch) + Tau(store) + (2Tau(fetch) + Tau(<) )*(N + 1) + (2Tau(fetch) + Tau(+) + Tau(store)) * N
fact = 1;
for( c = 1 ; c <= n ; c++ )
{
fact = fact*c;
}
like this?

Resources