UrbanPro
true

Learn Advanced Java coaching from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Simple Algorithms - Fibonacci, Finding GCD & Finding LCM.

Paras Chawla
29/05/2017 0 0

import java.util.Scanner;

 

//0,1,1,2,3,5,8,13,21,34...

//fun(n)=fun(n-1)+fun(n-2) where n>=2

/*fib(9)=fib(8)+fib(7)

=fib(7)+fib(6)+fib(6)+fib(5)

=fib(6)+fib(5)+fib(5)+fib(4)+fib(5)+fib(4)+fib(4)+fib(3)

*/

public class FibonacciNumberFast {

     public static void main(String[] args) {

           Scanner scanner = new Scanner(System.in);

           int n = scanner.nextInt();

           long fno=fibonacciNumber(n);

           System.out.println(fno);  

     }

     public  static long fibonacciNumber(int n){

           int array[]= new int[n+1];

           array[0]=0;

           if(n==0)

                return array[0];

           array[1]=1;

           for(int i=2;i<=n;i++)

                array[i]=array[i-1]+array[i-2];

           return array[n];

     }

}

 

Algorithm 2– Finding GCD of a number

 1) GCD using Recursion

                          2) GCD using naïve approach

 

import java.util.Scanner;

 

// Largest number which divides both a and b

public class GCDNaive {

     public static void main(String[] args) {

           Scanner scanner = new Scanner(System.in);

           long a = scanner.nextLong();

           long b = scanner.nextLong();

           //long gcd = gcd(a, b);

           //System.out.println("Result from GCD Naive "+gcd);

           gcdFast(a, b);

     }

     static void gcdFast(long a, long b) {

           if (b == 0) {

                System.out.println(a);

           System.exit(0);

           }

           long rem=a%b;

           gcdFast(b, rem);

     }

/*   static long gcd(long a, long b) {

           long best = 0;

           for (long i = 1; i <= a + b; i++) {

                if ((a%i ==0) && (b%i== 0))

                     best = i;

           }

           return best;

     }

*/}


Algorithm 3 – Finding LCM of a number

 

import java.util.Scanner;

 

// GCD(a,b)*LCM(a,b)=a*b

public class LCM {

     public static void main(String[] args) {

           Scanner scanner = new Scanner(System.in);

           long a = scanner.nextLong(); // 8

           long b = scanner.nextLong(); // 6

           // long c = gcd(a, b);

           // System.out.println("GCD is" + c);

           long c = gcdFast(a, b);

           long d = (a * b) / c;

           System.out.println(d);

     }

 

     static long gcdFast(long a, long b) {

           if (b == 0) {

                return a;

           }

           long rem = a % b;

           long c=gcdFast(b, rem);

           return c;

     }

 

     /*   static long gcd(long a, long b) {

           long best = 0;

           for (long i = 1; i <= a + b; i++) {

                if ((a % i == 0) && (b % i == 0))

                     best = i;

           }

           return best;

     }*/

}

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Methodology of Learning - specially mathematics
tt ALWAYS TAKE THE HELP OF THE INTERNET. EXCELLENT REFERENCE MATERIAL IS AVAILABLE HERE IN PRACTICALLY EVERY SUBJECT THAT YOU CAN THINK OF. DO NOT WITH ONLY ONE SITE, GO TO AS MANY SITES AS POSSIBLE...

Circular motion
Circular motion is rotation along a circle: a circular path or a circular orbit. It can be uniform, that is, with constant angular rate of rotation, or non-uniform, that is, with a changing rate of rotation. Examples...

Shear Force Diagram (SFD) & Bending Moment Diagram (BMD) for Cantilever Beam
Problem Statement Draw shear force and bending moment diagrams of the cantilever beam carrying point loads. As shownin figure: SOLUTION:Shear Force:To draw a shear force diagram, first find value...

Shortcut method of finding inverse of a 2x2 matrix useful for students of diploma CET & engineering CET.
Instead of going through conventional method of finding inverse of a 2 x 2 matrix, let us solve following example using shortcut method that is useful for students taking CET exams where time is a constraint. #...

Dialogue - Conversation between a Tree and a Human Being
Dialogue - Conversation between a Tree and a Human Being Tree: Hi, Human Being, How are you? I hope you are well.Human Being: I am fine, thank you and how are you?Tree: I’m not fine at all....
X

Looking for Advanced Java coaching Classes?

The best tutors for Advanced Java coaching Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Advanced Java coaching with the Best Tutors

The best Tutors for Advanced Java coaching Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more