完美平方数定义

如果一个正整数 a 是某一个整数 b 的平方,那么这个正整数 a 叫做完全平方数。零也可称为完全平方数。

完全平方数的性质如下:

1、平方数的个位数字只能是 0, 1,4,5,6,9 。

2、任何偶数的平方一定能被 4 整除;任何奇数的平方被 4(或 8)除余 1,即被4 除余 2 或 3 的数一定不是完全平方数。

3、完全平方数的个位数字是奇数时,其十位上的数字必为偶数。完全平方数的个位数字是 6 时,其十位数字必为奇数。

4、凡个位数字是 5 但末两位数字不是 25 的自然数不是完全平方数;末尾只有奇数个 0 的自然数不是完全平方数;个位数字是 1,4,9 而十位数字为奇数的自然数不是完全平方数。

5、除 1 外,一个完全平方数分解质因数后,各个质因数的指数都是偶数,如果一个数质分解后, 各个指数都为偶数, 那么它肯定是个平方数。 完全平方数的所有因数的总个数是奇数个。因数个数为奇数的自然数一定是完全平方数。

6、如果 a 、b 是平方数, a=bc ,那么 c 也是完全平方数。

7、两个连续自然数的乘积一定不是平方数,两个连续自然数的平方数之间不再有平方数。

8、如果十位数字是奇数,则它的个位数字一定是6;反之也成立。

测试几种方法的用时

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PerfectSquareDemo

{

class Program

{

const int maxNumber = 4096;

static void Main(string[] args)

{

Console.SetWindowSize(140, 20);

Console.WriteLine("小明今年的年龄减去10是个完全平方数,加上10也是个完全平方数,问小明今年的年龄");

List list = GetPerfectSquareList(1, 100);

for (int i = 0; i < 100; i++)

{

if (list.Contains(i - 10) && list.Contains(i + 10))

{

Console.WriteLine($"从完全平方数的集合中查找:小明年龄【{i}】");

}

if (IsPerfectSquareNumber(i - 10) && IsPerfectSquareNumber(i + 10))

{

Console.WriteLine($"根据完全平方数定义查找:小明年龄【{i}】");

}

}

Console.WriteLine("----测试几个算法的用时----");

Task taskPredicate = Task.Run(() => { AlgorithmTimespan(IsPerfectSquareNumber, nameof(IsPerfectSquareNumber)); });

Task taskPredicate1 = Task.Run(() => { AlgorithmTimespan(IsPerfectSquareNumber1, nameof(IsPerfectSquareNumber1)); });

Task taskPredicate2 = Task.Run(() => { AlgorithmTimespan(IsPerfectSquareNumber2, nameof(IsPerfectSquareNumber2)); });

Task.WaitAll(taskPredicate, taskPredicate1, taskPredicate2);

Console.ReadLine();

}

///

/// 获取完美平方数的集合

///

///

///

///

static List GetPerfectSquareList(int fromIndex, int toIndex)

{

List list = new List();

for (int i = fromIndex; i <= toIndex; i++)

{

list.Add(i * i);

}

return list;

}

///

/// 算法用时

///

/// 判定是否是完美平方数的方法

///

static void AlgorithmTimespan(Func PredicateMethod, string methodName)

{

Stopwatch stopwatch = Stopwatch.StartNew();

string path = AppDomain.CurrentDomain.BaseDirectory + methodName + ".log";

if (File.Exists(path))

{

File.Delete(path);

}

for (int number = 1; number <= maxNumber; number++)

{

bool isPerfectSquare = PredicateMethod(number);

File.AppendAllText(path, $"【{number}】是完美平方数:【{isPerfectSquare}】{Environment.NewLine}", Encoding.UTF8);

}

Console.WriteLine($"【{methodName.PadRight(22)}】,对【{maxNumber}】个数字进行完美平方数判定并打印出来,用时【{stopwatch.Elapsed.TotalMilliseconds}】ms");

}

///

/// 判断一个数是否是完全平方数

/// 平方根取整法

///

///

///

static bool IsPerfectSquareNumber(int number)

{

double x = Math.Sqrt(number);

int truncate = (int)x;

return truncate * truncate == number;

}

///

/// 判断一个数是否是完全平方数

/// 存在一个数的平方等于当前数

///

///

///

static bool IsPerfectSquareNumber1(int number)

{

for (int i = 1; i <= number; i++)

{

if (i * i == number)

{

return true;

}

}

return false;

}

///

/// 判断一个数是否是完全平方数

/// 取平方根,然后取整,如果能整除

///

///

///

static bool IsPerfectSquareNumber2(int number)

{

int i = (int)Math.Sqrt(number);

return number % i == 0;

}

}

}

运行结果: