数据结构和算法 数论 自守数

1、概述

        如果一个正整数的平方的末尾几位数等于这个数本身,那么这个数便称为自守数。自守数包含了很多特殊的性质。

        依照自守数的定义,很容易找到一些简单的自守数,例如:

        5是一个一位自守数,因为5^2=25,末尾仍然为5。

        6是一个一位自守数,因为6^2=36,末尾仍然为6。

        25是一个两位自守数,因为25^2=625,末尾两位仍然为25。

        76是一个两位自守数,因为76^2=5776,末尾两位仍然为76。

        625是一个三位自守数,因为625^2=390625,末尾两位仍然为625。

        376是一个三位自守数,因为376^2=141376,末尾两位仍然为376。

        ......

2、特殊性质

        1)以自守数为后几位的两数相乘,结果的后几位仍是这个自守数

        自守数的一个最基本的特点便是,以自守数为后几位的两数相乘,结果的后几位仍是这个自守数。例如,76是一个两位自守数,以76为后两位的两个数相乘,那么乘积的结果的后两位仍然是76,示例如下:

        176×576=101376

        276×376=103776

        2)n+1位的自守数出自n位的自守数

        在所有的自守数中,n+1位的自守数出自n位的自守数。例如:

        625是一个三位自守数,其末尾两位为25,仍然是一个自守数。而25是一个两位自守数,其末尾一位为5,而5是一个一位自守数。

        376是一个三位自守数,其末尾两位为76,仍然是一个自守数。而76是一个两位自守数,其末尾一位为6,而6是一个一位自守数。

        这样,如果知道了n位的自守数,那么在其前面增加一位数即可成为n+1位的自守数,在有的场合可以减轻计算量。

        3)两个n位自守数的和等于10^n+1

        两个n位自守数的和等于10^n+1,示例如下:

        5+6=11=10^1+1

        25+76=101=10^2+1

        625+376=1001=10^3+1

3、Java判断是否自守数

class Test {
	// Function to check Automorphic number
	static boolean isAutomorphic(int N)
	{
		// Store the square
		int sq = N * N;

		// Start Comparing digits
		while (N > 0) {
			// Return false, if any digit of N doesn't
			// match with its square's digits from last
			if (N % 10 != sq % 10)
				return false;

			// Reduce N and square
			N /= 10;
			sq /= 10;
		}

		return true;
	}

	// Driver method
	public static void main(String[] args)
	{
		int N = 5;

		System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");
	}
}

4、c#判断是否自守数

// C# program to check if a
// number is Automorphic
using System;

class GFG {

	// Function to check Automorphic number
	static bool isAutomorphic(int N)
	{

		// Store the square
		int sq = N * N;

		// Start Comparing digits
		while (N > 0) {
			// Return false, if any digit
			// of N doesn't match with its
			// square's digits from last
			if (N % 10 != sq % 10)
				return false;

			// Reduce N and square
			N /= 10;
			sq /= 10;
		}

		return true;
	}

	// Driver Code
	public static void Main()
	{
		int N = 5;

		Console.Write(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");
	}
}

5、c++判断是否自守数

// C++ program to check if a number is Automorphic
#include <iostream>
using namespace std;

// Function to check Automorphic number
bool isAutomorphic(int N)
{
	// Store the square
	int sq = N * N;

	// Start Comparing digits
	while (N > 0) {
		// Return false, if any digit of N doesn't
		// match with its square's digits from last
		if (N % 10 != sq % 10)
			return false;

		// Reduce N and square
		N /= 10;
		sq /= 10;
	}

	return true;
}

// Driver code
int main()
{
	int N = 5;

	isAutomorphic(N) ? cout << "Automorphic"
					: cout << "Not Automorphic";

	return 0;
}

猜你喜欢

转载自blog.csdn.net/bashendixie5/article/details/125187572