给定整数n 。检查数字是否可重构。可重构数字是一个整数n,该整数可以通过其所有除数的计数来整除。
例子 :
Input: n = 8
Output: yes
Explanation:
8 has 4 divisors: 1, 2, 4, 8
Since 8 is divisible by 4 therefore 8 is
refactorable number.
Input : n = 4
Output: no
该解决方案非常简单。这个想法是从1迭代到sqrt(n)并计算一个数字的所有除数。之后,我们只需要检查数字n是否可被总数除以即可。
C++
// C++ program to check whether number is
// refactorable or not
#include
// Function to count all divisors
bool isRefactorableNumber(int n)
{
// Initialize result
int divCount = 0;
for (int i = 1; i <= sqrt(n); ++i)
{
if (n % i==0)
{
// If divisors are equal, count
// only one.
if (n / i == i)
++divCount;
// Otherwise count both
else
divCount += 2;
}
}
return n % divCount == 0;
}
//Driver Code
int main()
{
int n = 8;
if (isRefactorableNumber(n))
puts("yes");
else
puts("no");
n = 14;
if (isRefactorableNumber(n))
puts("yes");
else
puts("no");
return 0;
}
Java
// Java program to check whether number is
// refactorable or not
class GFG
{
// Function to count all divisors
static boolean isRefactorableNumber(int n)
{
// Initialize result
int divCount = 0;
for (int i = 1; i <= Math.sqrt(n); ++i)
{
if (n % i==0)
{
// If divisors are equal, count
// only one.
if (n / i == i)
++divCount;
// Otherwise count both
else
divCount += 2;
}
}
return n % divCount == 0;
}
public static void main (String[] args)
{
int n = 8;
if (isRefactorableNumber(n))
System.out.println("yes");
else
System.out.println("no");
n = 14;
if (isRefactorableNumber(n))
System.out.println("yes");
else
System.out.println("no");
}
}
// This code is contributed by Saket Kumar
Python
# Python program to check whether number is
# refactorable or not
import math
def isRefactorableNumber(n):
# Initialize result
divCount = 0
for i in range(1,int(math.sqrt(n))+1):
if n % i == 0:
# If divisors are equal, count only one
if n/i == i:
divCount += 1
else: # Otherwise count both
divCount += 2
return n % divCount == 0
# Driver Code
n = 8
if isRefactorableNumber(n):
print "yes"
else:
print "no"
n = 14
if (isRefactorableNumber(n)):
print "yes"
else:
print "no"
C#
// C# program to check whether number is
// refactorable or not
using System;
class GFG
{
// Function to count all divisors
static bool isRefactorableNumber(int n)
{
// Initialize result
int divCount = 0;
for (int i = 1; i <= Math.Sqrt(n); ++i)
{
if (n % i==0)
{
// If divisors are equal, count
// only one.
if (n / i == i)
++divCount;
// Otherwise count both
else
divCount += 2;
}
}
return n % divCount == 0;
}
// Driver code
public static void Main ()
{
int n = 8;
if (isRefactorableNumber(n))
Console.WriteLine("yes");
else
Console.Write("no");
n = 14;
if (isRefactorableNumber(n))
Console.Write("yes");
else
Console.Write("no");
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
Output:
yes
no
时间复杂度: O(sqrt(n))
辅助空间: O(1)
关于可重构数的事实
- 没有完美的可重构数字。
- 没有三个连续的整数都可以重构。
- 重构数的自然密度为零。