Coursera's Algorithmic toolbox assignment solutions( Last Digit of the Sum of Fibonacci Numbers Again, Last Digit of the Sum of Squares of Fibonacci Numbers ) :

Problem 7:  Last Digit of the Sum of Fibonacci Numbers Again

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include <bits/stdc++.h>
using namespace std;
long long unsigned fib(long long unsigned p) //function to calculate nth fib. number
{
long long unsigned f[p],i;
f[0]=0;
f[1]=1;
for(i=2;i<=p;i++)
{
f[i]=(f[i-1]+f[i-2])%10;
}
return f[p];
}
int main()
{
long long unsigned n,i,sum=0,m;
cin>>n>>m;
for(i=n;i<=m;i++)
{
sum+=fib(i);
}
cout<<sum%10;
}

This code is simple. There's no need for any explanation.

 

 

 

 

Problem 8: Last Digit of the Sum of Squares of Fibonacci Numbers

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

Still working on this solution...

But for small test cases this code will work fine.

#include <bits/stdc++.h>
using namespace std;
int main()
{
long long unsigned n,i,p,q;
cin>>n;
long long unsigned f[n];
f[0]=0;
f[1]=1;
for(i=2;i<=n+1;i++)
{
f[i]=(f[i-1]+f[i-2])%10;
p=f[n]%10;
q=f[n+1]%10;
}
cout<<(p*q)%10;
}

 

 

 

Guys , if you have any queries related to a question or need more explanation, comment down below!

 

Comments

  1. Problem number 8 is for small input only it took about a minute for 1234567890
    I'm searching for fastest program. Can you make one please.......

    ReplyDelete

Post a Comment

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :