Some of the classic algorithms in Java

Source: Internet
Author: User

Package Com.xfm.algorithm;

Import Java.util.ArrayList;
Import java.util.List;

/**
* Some classic algorithms in Java (algorithm)
*@authorXfm
*/
public class Classicjavaalgorithm {
/**
* Ask the Fibonacci number.
* Ask two, there is a pair of rabbits, from the 3rd month after birth every month, a pair of rabbits,
* The rabbit grows to a third month after a pair of rabbits, if the rabbit is not dead,
* What is the total number of rabbits per month?
* 1 1 2 3 5 8 13 ...
*@paramN
*@return
*/
public static Long GetNumber (int n) {
if (N <0) {
Return-1L;
}else if (n = =0) {
Return0L;
}else if (n = =1 | | n = =2) {
Return1L;
}else{
ReturnGetNumber (N-1) +GetNumber (N-2);
}
}
public static Long Getfib (int n) {
return n <=0?-1l:n = =1| | n = =2?1 L:GETFIB (n1) +GETFIB (n2);
}

/**
* Test recursive algorithm compared to traditional cycle efficiency
*/

public static final intMONTH =40;
  
public static voidFib_test () {
Long start = System.Currenttimemillis ();
System.Out.println (GETFIB (MONTH));
System.Out.println (System.Currenttimemillis ()-start);
Long Start1 = System.Currenttimemillis ();
Long F1 =1L, F2 =1L;
Long F;
Forint i=3; i<=MONTH; i++) {
f = F2;
F2 = f1 + F2;
F1 = f;
System.Out.print ("No." + i +"Months of the Rabbit logarithm:");
System.Out.println ("" + F2);
}
System.Out.println (System.Currenttimemillis ()-start1);
}

/**
* In summary, the recursive algorithm is much less efficient than the normal loop algorithm
*/

/**
* Figure 100-999 of Daffodils
* Print out all the daffodils (Narcissus number), the so-called "Narcissus number" refers to a three-digit number,
* Its numbers are cubic and equal to the number itself.
* For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of three +3 Times Square.
* Result 153 370 371 407
*/
public static void Narcissus () {
for (int i =100; I <1000; i++) {
int one = i/100;
int = i/10%10;
int thr = i-i/10*10;
int res = ONE*ONE*ONE+TWO*TWO*TWO+THR*THR*THR;
if (res = = i) {
System.Out.println (i);
}
}
}

/**
* Title: Get all prime numbers within a number (primes)
*@paramSize
*/
public static list<integer> Getprime (int size) {
list<integer> integers =NewArraylist<> ();
for (int i =2; i < size; i++) {
Boolean f =True
for (Int J =2; J < I; J + +) {
if (i%j = =0) {
f =False
}
}
if (f) {
Integers.add (i);
}

}
return integers;
}
/**
* Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
* Factoring (factorization)
*@paramNum
*/
public static void Factorization (int num) {
int orinnum = num;
List<integer> is =Getprime (num+1);
list<integer> res =NewArraylist<> ();
Forint i =0;i<is.size (); i++) {
Integer integer = is.get (i);
if (Num%integer = =0) {
Res.add (integer);
num = Num/integer;
i =0;
}
}

StringBuffer SF =New StringBuffer ();
Sf.append (orinnum+"=");
for (int i =0; I < res.size (); i++) {
if (I<res.size ()-1) {
Sf.append (Res.get (i) +"*");
}else {
Sf.append (Res.get (i) +"");
}
}
System.Out.println (Sf.tostring ());
}

/**
* Title: Enter two positive integers and ask for a least common multiple.
*@paramOne
*@paramBoth
*@return
*/
public static int Leastcommonmultiple (int one,int) {
int max = one>two?one:two;
List<integer> is =getprime (max);
for (Integer integer:is) {
if (One%integer = = 0 && two%integer = 0) {
return integer;
}
}
return-1;} /** * Title: Enter two positive integers and ask for a greatest common divisor. * @param one * @param/public static int maxcommondivisor (int one,int) {int max = one*two; int min = One>two ? one:two; for (int i = min; i < Max; i++) {if (I%one = = 0 && I%two = = 0) {return i;}} return Max; public static void Main (string[] args) {int one = 999; int = 45; System.out.println (One*two); int a = Maxcommondivisor (One,two); System.out.println (a); }}

Some of the classic algorithms in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.