📜  卡普雷卡编号

📅  最后修改于: 2021-04-30 03:05:26             🧑  作者: Mango

Kaprekar数是一个数字,当将其分为两部分时,其平方为零,使得部分的总和等于原始数字,并且所有部分的值都不为0。(来源:Wiki)
给定一个数字,任务是检查它是否是Kaprekar数字。
例子:

Input :  n = 45  
Output : Yes
Explanation : 452 = 2025 and 20 + 25 is 45
 
Input : n = 13
Output : No
Explanation : 132 = 169. Neither 16 + 9 nor 1 + 69 is equal to 13

Input  : n = 297  
Output : Yes
Explanation:  2972 = 88209 and 88 + 209 is 297

Input  : n = 10 
Output : No
Explanation:  102 = 100. It is not a Kaprekar number even if
sum of 100 + 0 is 100. This is because of the condition that 
none of the parts should have value 0.
  1. 求n的平方并计算平方中的位数。
  2. 在不同位置拆分正方形,并查看任何拆分中的两个部分的总和是否等于n。

以下是该想法的实现。

C++
//C++ program to check if a number is Kaprekar number or not
#include
using namespace std;
 
// Returns true if n is a Kaprekar number, else false
bool iskaprekar(int n)
{
    if (n == 1)
    return true;
 
    // Count number of digits in square
    int sq_n = n * n;
    int count_digits = 0;
    while (sq_n)
    {
        count_digits++;
        sq_n /= 10;
    }
 
    sq_n = n*n; // Recompute square as it was changed
 
    // Split the square at different poitns and see if sum
    // of any pair of splitted numbers is equal to n.
    for (int r_digits=1; r_digits


Java
// Java program to check if a number is
// Kaprekar number or not
 
class GFG
{
    // Returns true if n is a Kaprekar number, else false
    static boolean iskaprekar(int n)
    {
        if (n == 1)
           return true;
      
        // Count number of digits in square
        int sq_n = n * n;
        int count_digits = 0;
        while (sq_n != 0)
        {
            count_digits++;
            sq_n /= 10;
        }
      
        sq_n = n*n; // Recompute square as it was changed
      
        // Split the square at different poitns and see if sum
        // of any pair of splitted numbers is equal to n.
        for (int r_digits=1; r_digits


Python
# Python program to check if a number is Kaprekar number or not
 
import math
 
# Returns true if n is a Kaprekar number, else false
def iskaprekar( n):
    if n == 1 :
        return True
     
    #Count number of digits in square
    sq_n = n * n
    count_digits = 1
    while not sq_n == 0 :
        count_digits = count_digits + 1
        sq_n = sq_n / 10
     
    sq_n = n*n  # Recompute square as it was changed
     
    # Split the square at different poitns and see if sum
    # of any pair of splitted numbers is equal to n.
    r_digits = 0
    while r_digits< count_digits :
        r_digits = r_digits + 1
        eq_parts = (int) (math.pow(10, r_digits))
         
        # To avoid numbers like 10, 100, 1000 (These are not
        # Karprekar numbers
        if eq_parts == n :
            continue
         
        # Find sum of current parts and compare with n
         
        sum = sq_n/eq_parts + sq_n % eq_parts
        if sum == n :
            return True
     
    # compare with original number
    return False
     
# Driver method
i=1
while i<10000 :
    if (iskaprekar(i)) :
        print i," ",
    i = i + 1
# code contributed by Nikita Tiwari


C#
// C# program to check if a number is
// Kaprekar number or not
using System;
 
class GFG {
     
    // Returns true if n is a Kaprekar
    // number, else false
    static bool iskaprekar(int n)
    {
        if (n == 1)
            return true;
 
        // Count number of digits
        // in square
        int sq_n = n * n;
        int count_digits = 0;
        while (sq_n != 0) {
            count_digits++;
            sq_n /= 10;
        }
 
        // Recompute square as it was changed
        sq_n = n * n;
         
        // Split the square at different poitns
        // and see if sum of any pair of splitted
        // numbers is equal to n.
        for (int r_digits = 1; r_digits < count_digits;
                                            r_digits++)
        {
             
            int eq_parts = (int)Math.Pow(10, r_digits);
 
            // To avoid numbers like 10, 100, 1000
            // These are not Karprekar numbers
            if (eq_parts == n)
                continue;
 
            // Find sum of current parts and compare
            // with n
            int sum = sq_n / eq_parts + sq_n % eq_parts;
            if (sum == n)
                return true;
        }
 
        // compare with original number
        return false;
    }
 
    // Driver method
    public static void Main()
    {
         
        Console.WriteLine("Printing first few "
        + "Kaprekar Numbers using iskaprekar()");
 
        for (int i = 1; i < 10000; i++)
            if (iskaprekar(i))
                Console.Write(i + " ");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

Printing first few Kaprekar Numbers using iskaprekar()
1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999