boring integer

1. Complete the number

A number is called perfect if it is exactly equal to the sum of its factors.

6 = 1 + 2 + 3

28 = 1 + 2 + 4 + 7 + 14

The process of finding all complete numbers within 10000:

(1) Divide all integers between 1 and n by n, and accumulate the dividends that are divisible.

(2) Finally, determine whether the sum of the factors is equal to the number n. If they are equal, the number n is the complete number, and the number and each factor are output.

	public static void main(String[] args) {
		int i, j, sum;
		sum = 0;
		for (i = 1; i <= 1000; i++) {
			for (j = 1; j < i; j++) {
				if (i % j == 0) {
					sum = sum + j;
				}
			}
			if (sum == i) {
				System.out.println(sum);
			}
			sum = 0;
		}
	}

 

 

2. Intimacy number

Suppose there are two numbers a and b. If the sum of all factors of a is equal to the sum of all factors of b, and a is not equal to b, then

 

Call a and b a pair of close numbers, such as 284 and 220 are a pair of close numbers.

To find the number of intimacy within 10000, the following algorithm can be used:

(1) For each number a, decompose the factor, save the factor into an array, and then divide the factor

 

The sum is saved to variable b1.

(2) The sum of the strong factors b1 is then factorized, and the factors are stored in an array. The sum of the factors

 

Save it to variable b2.

(3) If b2 is equal to a, and b1 is not equal to b2, find a pair of intimate numbers a and b1, which can be output.

(4) Repeat steps (1)-(3) to find the intimacy number in the specified range.

	public static void main(String[] args) {
		int a, i, b, n;
		for (a = 1; a < 8000; a++) {
			for (b = 0, i = 1; i <= a / 2; i++) {
				if ((a % i) == 0) {
					b += i;
				}
			}
			for (n = 0, i = 1; i <= b / 2; i++) {
				if ((b % i) == 0) {
					n += i;
				}
			}
			if (n == a && a < b) {
				System.out.println(a+"  AND  "+b);
			}
		}
	}

 

3. Number of daffodils

A three-digit number, if the value is equal to the sum of the three powers of the digits, it is called the daffodil number

	public static void main(String[] args) {
		for (int i = 100; i <= 999; i++) {
			int a = i / 100;
			int b = i % 100 / 10;
			int c = i % 10;
			if (a * a * a + b * b * b + c * c * c == i) {
				System.out.println("Number of daffodils:" + i);
			}
		}
	}

 

4. Self-defense numbers

The so-called autonomic number means that the square of a number is equal to the natural number of the number itself. For example: the square of 6 is 36, and the mantissa is 6, so 6 is an autonomic number; the square of 25 is equal to 625, and the mantissa is 25. So 25 is an autonomous number. (There are only 9 autonomous numbers between 1-200000)

	public static void main(String[] args) {
		for (int i = 1; i < 10000; i++) {
			String strI = String.valueOf(i);
			String multiStr = String.valueOf(i * i);
			String last = multiStr.substring(multiStr.length() - strI.length());
			if (last.equals(strI)) {
				System.out.println(i + "*" + i + "=" + multiStr + "--> " + i + " 是自守数");
			}
		}
	}

 

 

5. Greatest common divisor and least common multiple

 

> Euclidean algorithm:

The Euclidean algorithm uses the method of rolling and dividing to find the greatest common divisor, which is a traditional algorithm for calculating the greatest common divisor of two numbers.

Algorithm idea:

(1) For two known numbers m, n, make m>n;

(2) m is divided by n to get the remainder r;

(3) If r=0, then n is the obtained greatest common divisor, skip to (5) to find the least common multiple, otherwise go to (4)

(4) Save the value of n to m, save the value of r to n, and repeat steps (2) (3)

(5) With the greatest common divisor of two numbers, the least common multiple is very simple, just divide the product of the multiplication of two numbers by the greatest common divisor.

	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		System.out.println("Please enter the first number");
		int a = sca.nextInt ();
		System.out.println("Please enter the second number");
		int b = sca.nextInt();
		int c;
		if (a > b) {
			c = b;
		} else {
			c = a;
		}

		for (int d = c; d >= 1; d--) {
			if (a % d == 0 && b % d == 0) {
				System.out.println("The greatest common multiple is: " + d);
				System.out.println("The LCM is: " + (a * b / d));
				break;
			}
		}

	}

 

>Stein algorithm

Stein's algorithm only has integer shift and addition and subtraction, without the need for division and modulo operations, which will improve the execution efficiency of the algorithm. However , it is not only fast, but also solves the problem of Euclidean algorithm for finding the greatest common convention of two large numbers. Inconvenient to count.

Stein's algorithm is as follows (find the greatest common divisor of two numbers a and b).

	public static void main(String[] args) {
		System.out.println("The greatest common divisor is: "+Stein(56, 456));
	}

	static int Stein(int x, int y) {
		int factor = 0;
		int temp;
		if (x < y) {
			temp = x;
			x = y;
			y = temp;
		}
		if (0 == y) {
			return 0;
		}
		while (x != y) {
			if ((x & 1)!=0) {
				if ((y & 1)!=0)  {
					y = (x - y) >> 1;
					x -= y;
				} else {
					y >>= 1;
				}
			} else {
				if ((y & 1)!=0)  {
					x >>= 1;
					if (x < y) {
						temp = x;
						x = y;
						y = temp;
					}
				} else {
					x >>= 1;
					y >>= 1;
					++factor;
				}
			}
		}
		return (x << factor);
	}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326549005&siteId=291194637