High School TopCoder: Green Wood High School is set to premier a programming tournament for high school-aged math and science students across the country. Based on this contest, the school has called in for all the interested candidates to take up a qualifying test at the school premises.
Before the qualifier, the Event coordinators chose the problemsets and wanted to code it beforehand to ease the evaluation procedure. They wanted your help to code few of the problemsets, one of which was involving Fibonacci series. We all know the Fibonacci sequence, each term of it is the sum of the two previous terms. In this problem, we need to find just the last digit of a Fibonacci series termed as F(n), where n is got as input.
Here, create a class named Fibonacci with the following method.
Method Name | Description |
int fiboLastDigit(int) | This method should return the last digit of the term F (n). |
Create a driver class called Main. In the Main method, obtain input from the user in the console and display the the last digit of the term F (n) by calling the fiboLastDigit method present in the Fibonacci class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
Input Format:
The input consists of an integer n(0 <= n <= 10000).
Output Format:
Output the last digit of the term F (n).
Refer sample input and output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
5
Sample Input 2:
9
Sample Output 2:
4
Solution
import java.io.*;
import java.util.*;
import java.lang.*;
class Main {
//sauravhathi
static long fib(long n) {
long F[][] = new long[][] { { 1, 1 }, { 1, 0 } };
if (n == 0)
return 0;
power(F, n - 1);
return F[0][0];
}
static void multiply(long F[][], long M[][]) {
long x = F[0][0] * M[0][0] +
F[0][1] * M[1][0];
long y = F[0][0] * M[0][1] +
F[0][1] * M[1][1];
long z = F[1][0] * M[0][0] +
F[1][1] * M[1][0];
long w = F[1][0] * M[0][1] +
F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
//sauravhathi
static void power(long F[][], long n) {
if (n == 0 || n == 1)
return;
long M[][] = new long[][] { { 1, 1 }, { 1, 0 } };
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
multiply(F, M);
}
// sauravhathi
long fiboLastDigit(long n) {
return (fib(n) % 10);
}
public static void main(String[] args) {
Main ob = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(ob.fiboLastDigit(n));
}
//sauravhathi
}
#include <iostream>
using namespace std;
int fibo(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibo(n - 1) + fibo(n - 2);
}
int fiboLastDigit(int n)
{
//sauravhathi
return fibo(n) % 10;
}
int main()
{
int n;
cin >> n;
cout << fiboLastDigit(n) << endl;
return 0;
}
def fibo(n):
if n == 0:
return 0
#sauravhathi
elif n == 1:
return 1
else:
return fibo(n - 1) + fibo(n - 2)
def fiboLastDigit(n):
#sauravhathi
return fibo(n) % 10
n = int(input())
print(fiboLastDigit(n))
Happy Learning – If you require any further information, feel free to contact me.