original link: http://liuqing-2010-07.iteye.com/blog/1396859
1. How do I calculate leap years (leap year)?
Four years a leap, a century does not leap, 400 years again leap.
Specific reference: http://baike.baidu.com/view/3085625.htm
Java code
- Boolean isleapyear (int year) {
- return (year%4 = = 0 && year%!!=0) | | (year%= = 0);
- }
2. How can I tell if a number is prime?
Prime numbers are also called primes. Refers to the number of natural numbers greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself.
Specific reference: http://baike.baidu.com/view/1767.htm
Java code
- Boolean isprimenumber (int n) {
- int n1 = (int) math.sqrt (n);
- For (int i = 2; I <= N1; i++) {
- if (n%i = = 0) {
- return false;
- }
- }
- return true;
- }
Note: Here n must be greater than 1.
3. Decompose factorization (decomposition of prime factors).
Each composite can be written in several prime-number multiplication forms. Each of these prime numbers is the composite factor, called the decomposition factorization of this composite. The number of decomposed mass is only for composite.
The factorization of a number decomposition is to be removed from the smallest prime numbers until the result is prime. Short division seeking mass factor:
Specific reference: Http://baike.baidu.com/view/832102.htm#1
Java code
- Returns an array of factorization
- Integer[] Decprime (int n) {
- list<integer> list = new arraylist<integer> ();
- For (int i=2;i <= n;i++) {
- While (n! = i) {
- if (n%i! = 0) {
- Break ; //Can not divide is definitely not a factor, divisible here must be prime. Because all the 2,3,5,7
- //have been removed after the completion. The remaining factor can only be an odd number and a prime number.
- }
- List.add (integer.valueof (i));
- n = n/i;
- }
- }
- List.add (integer.valueof (n));
- return List.toarray (new Integer[list.size ()));
- }
4. Ask for two numbers of greatest common divisor (greatest Common Divisor).
If a natural number a can be divisible by the natural number B, then a is a multiple of B and B is an approximate of a. Several natural numbers of the public approximate number, called these natural number of conventions. The largest number of conventions in the Convention, known as the greatest common divisor of these natural numbers.
As early as 300 A.D., Euclid had given an efficient solution--the Division method--in his book, "Geometry originally". The principle of the greatest common divisor is very clever and simple, assuming that f (x, y) represents x, Y, k = x/y,b = X%y, then x = KY + B, if a number can be divisible by both X and Y, then both B and Y must be divisible, and the numbers that are divisible by both B and Y must be divisible by both X and Y. That is, the number of conventions for x and Y is the same as the number of Conventions B and Y, and its greatest common divisor is the same, then there is f (x, y) = f (y, x%y) (Y > 0), so that the original problem can be converted to two decimal greatest common divisor, until one of the numbers is 0, The other number remaining is the largest number of conventions.
Specific reference: http://baike.baidu.com/view/47637.htm
Java code
- Long gcd (long x,long y) {
- if (x < y) {
- long m = x;
- x = y; //x Stores a large number of
- y = m;
- }
- long k = 0;
- While (Y! = 0) {
- K = x%y;
- x = y;
- y = k;
- }
- return x;
- }
5. Ask for two number of least common multiple (Least Common multiple).
Several numbers of public multiples are called the common multiple of these numbers, the smallest of which is called the least common multiple of these few numbers. The least common multiple of natural number A and B can be recorded as [A, a], the maximum common factor of natural number A and B can be recorded as (A, a), when (A, b) =1, [a,b]= axb.
The largest common factor and least common multiple of two numbers have the following relationships:
The product of the maximum male factor x least common multiple = two number
That is (A, b) x[a,b]= Axb.
Proof: Set a = X* (A, B), B. y* (A, a) where x, y does not exist in the Convention number.
A * b = [x * (A, B)] * [y * (A, B)]
= [x * y * (A, B)] * (A, B)
= [A, b] * (A, B)
Specific reference: Http://baike.baidu.com/view/341375.htm#2
Java code
- Long LCM (long x,long y) {
- return x*y/gcd (x, y);
- }
6. Finish (Perfect number).
The number, that is, the perfect number, a number if exactly equal to the sum of factors other than itself, this number is called the completion number. such as 6=1+2+3. (6 of the factor is the
Specific reference: http://baike.baidu.com/view/640632.htm
Http://zjtys-2006.blog.sohu.com/164964518.html
Java code
- Boolean Isperfectnumber (long N) {
- int s = 0;
- //Iterate to find the sum of all the factors
- For (int i = 1; i < n; i++) {
- if (n%i = = 0) {
- s +=i;
- }
- }
- //Determine if the number is equal to the sum of its factors, and the number is the end.
- if (s = = N) {
- return true;
- }
- return false;
- }
7. Daffodils (daffodils number).
Narcissus number refers to an n-digit number (n≥3), and the sum of the number of its digits on each bit is equal to its own. (Example: 1^3 + 5^3 + 3^3 = 153)
Specific reference: http://baike.baidu.com/view/152077.htm
Java code
- The algorithm uses the basic data type and can only calculate a small number of "daffodils". It is suggested that interested readers use BigInteger to find "Narcissus number".
- Boolean Isdaffodilsnumber (long l) {
- Char [] c = string.valueof (L). ToCharArray ();
- int d = 0,n = c.length;
- long sum = 0;
- For (int i = 0; i < c.length; i++) {
- D = c[i]-' 0 ';
- Sum + = Math.pow (d, N); //There may be a loss of precision
- }
- if (sum = = L) {
- return true;
- }
- return false;
- }
8. Palindrome numbers (palindrome number).
If a number (first not 0) is read from left to right and read from right to left, we call it a palindrome number.
For example: 121,323 .....
The method for judging whether a number is a palindrome is as follows:
Java code
- Boolean Ispalindromenumber (long l) {
- Char [] c = string.valueof (L). ToCharArray ();
- int len = c.length/2;
- For (int i = 0; i < len; i++) {
- if (c[i]! = c[c.length-1-i]) {
- return false;
- }
- }
- return true;
- }
9. Newton Iterative method to find square root (Newton's method).
The computer itself can not open the square, open square operation is implemented by the program.
Specific reference: Http://baike.baidu.com/view/643093.htm#2
Http://wenku.baidu.com/view/6b74c622bcd126fff7050bfe.html
Java code
- /**
- * Seek square root
- * @param d to prescribe
- * @param accuracy of precision arithmetic square root
- * @return
- */
- Double sqrt (double D,double precision) {
- Double x1 = d/2, x2 = (x1 + d/x1)/2;
- While (Math.Abs (x2-x1) >precision) {
- x1 = x2;
- x2 = (x1 + d/x1)/2;
- }
- return x1;
- }
10. Odd Even quick judgment (odd even).
The method of judging odd and even numbers is generally divided by 2 or 2 modulo, the result being 0 is even and the inverse is odd.
The number of internal numbers in the computer is represented by binary, and the odd minimum bit must be 1, even 0. Based on this feature, you can
The use of bitwise AND operation of the odd even judgment.
Java code
- Returns true if it is odd, otherwise an even number returns false.
- Boolean isodd (long l) {
- if ((L & 0x01) = = 0) {
- return false;
- }
- return true;
- }
Note: The division operation speed < modulus speed < bitwise AND operation speed in the computer
Java implementation of classical algorithm problem (i.)