6174是Kaprekar常数。此数字很特殊,因为对于任何四位数的数字,按照以下步骤进行操作时,我们总是会得到此数字,以使数字的所有数字都不相同,即,所有四位数的数字除外(0000、1111,…)
- 按升序对四位数进行排序,并将结果存储在数字“ asc”中。
- 按降序对四位数字进行排序,并将结果存储在数字“ desc”中。
- 用较小的数字减去较大的数字,即abs(asc – desc)。
- 重复以上三个步骤,直到减法的结果不等于前一个数字。
我们总是以6174结尾。
以下是演示相同程序。
C++
// C++ program to demonstrate working of
// Kaprekar constant
#include
using namespace std;
// This function checks validity of kaprekar's
// constant. It returns kaprekar's constant for
// any four digit number "n" such that all digits
// of n are not same.
int kaprekarRec(int n, int &prev)
{
if (n == 0)
return 0;
// Store current n as previous number
prev = n;
// Get four digits of given number
int digits[4];
for (int i=0; i<4; i++)
{
digits[i] = n%10;
n = n/10;
}
// Sort all four dgits in ascending order
// And giet in the form of number "asc"
sort(digits, digits+4);
int asc = 0;
for (int i=0; i<4; i++)
asc = asc*10 + digits[i];
// Get all four dgits in descending order
// in the form of number "desc"
sort(digits, digits+4, std::greater ());
int desc = 0;
for (int i=0; i<4; i++)
desc = desc*10 + digits[i];
// Get the difference of two numbers
int diff = abs(asc - desc);
// If difference is same as previous, we have
// reached kaprekar's constant
if (diff == prev)
return diff;
// Else recur
return kaprekarRec(diff, prev);
}
// A wrapper over kaprekarRec()
int kaprekar(int n)
{
int prev = 0;
return kaprekarRec(n, prev);
}
// Driver code
int main()
{
// Trying few four digit numbers, we
// always get 6174
cout << kaprekar(1000) << endl;
cout << kaprekar(1112) << endl;
cout << kaprekar(9812) << endl;
return 0;
}
Java
// Java program to demonstrate working of
// Kaprekar constant
import java.util.Arrays;
class GFG{
// This function checks validity of kaprekar's
// constant. It returns kaprekar's constant for
// any four digit number "n" such that all digits
// of n are not same.
static int kaprekarRec(int n, int prev)
{
if (n == 0)
return 0;
// Store current n as previous number
prev = n;
// Get four digits of given number
int[] digits=new int[4];
for (int i=0; i<4; i++)
{
digits[i] = n%10;
n = n/10;
}
// Sort all four dgits in ascending order
// And giet in the form of number "asc"
Arrays.sort(digits);
int asc = 0;
for (int i=0; i<4; i++)
asc = asc*10 + digits[i];
// Get all four dgits in descending order
// in the form of number "desc"
Arrays.sort(digits);
int desc = 0;
for (int i=3; i>=0; i--)
desc = desc*10 + digits[i];
// Get the difference of two numbers
int diff = Math.abs(asc - desc);
// If difference is same as previous, we have
// reached kaprekar's constant
if (diff == prev)
return diff;
// Else recur
return kaprekarRec(diff, prev);
}
// A wrapper over kaprekarRec()
static int kaprekar(int n)
{
int prev = 0;
return kaprekarRec(n, prev);
}
// Driver code
public static void main(String[] args)
{
// Trying few four digit numbers, we
// always get 6174
System.out.println(kaprekar(1000));
System.out.println(kaprekar(1112));
System.out.println(kaprekar(9812));
}
}
// This code is contributed by mits
Python3
# Python3 program to demonstrate
# working of Kaprekar constant
# This function checks validity of
# kaprekar's constant. It returns
# kaprekar's constant for any four
# digit number "n" such that all
# digits of n are not same.
def kaprekarRec(n, prev):
if (n == 0):
return 0;
# Store current n as previous
# number
prev = n;
# Get four digits of given number
digits = [0] * 4;
for i in range(4):
digits[i] = n % 10;
n = int(n / 10);
# Sort all four dgits in ascending order
# And giet in the form of number "asc"
digits.sort();
asc = 0;
for i in range(4):
asc = asc * 10 + digits[i];
# Get all four dgits in descending order
# in the form of number "desc"
digits.sort();
desc = 0;
for i in range(3, -1, -1):
desc = desc * 10 + digits[i];
# Get the difference of two numbers
diff = abs(asc - desc);
# If difference is same as previous,
# we have reached kaprekar's constant
if (diff == prev):
return diff;
# Else recur
return kaprekarRec(diff, prev);
# A wrapper over kaprekarRec()
def kaprekar(n):
rev = 0;
return kaprekarRec(n, rev);
# Driver code
# Trying few four digit numbers,
# we always get 6174
print(kaprekar(1000));
print(kaprekar(1112));
print(kaprekar(9812));
# This code is contributed by mits.
C#
// C# program to demonstrate working of
// Kaprekar constant
using System;
class GFG{
// This function checks validity of kaprekar's
// constant. It returns kaprekar's constant for
// any four digit number "n" such that all digits
// of n are not same.
static int kaprekarRec(int n, int prev)
{
if (n == 0)
return 0;
// Store current n as previous number
prev = n;
// Get four digits of given number
int[] digits=new int[4];
for (int i=0; i<4; i++)
{
digits[i] = n%10;
n = n/10;
}
// Sort all four dgits in ascending order
// And giet in the form of number "asc"
Array.Sort(digits);
int asc = 0;
for (int i=0; i<4; i++)
asc = asc*10 + digits[i];
// Get all four dgits in descending order
// in the form of number "desc"
Array.Sort(digits);
int desc = 0;
for (int i=3; i>=0; i--)
desc = desc*10 + digits[i];
// Get the difference of two numbers
int diff = Math.Abs(asc - desc);
// If difference is same as previous, we have
// reached kaprekar's constant
if (diff == prev)
return diff;
// Else recur
return kaprekarRec(diff, prev);
}
// A wrapper over kaprekarRec()
static int kaprekar(int n)
{
int prev = 0;
return kaprekarRec(n, prev);
}
// Driver code
public static void Main()
{
// Trying few four digit numbers, we
// always get 6174
System.Console.WriteLine(kaprekar(1000));
System.Console.WriteLine(kaprekar(1112));
System.Console.WriteLine(kaprekar(9812));
}
}
// This code is contributed by mits
PHP
Javascript
输出 :
6174
6174
6174
插图 :
n = 2324
1) asc = 2234
2) desc = 4322
3) Difference = 2088
4) Repeating above steps as difference is not same
as n
n = 2088
1) asc = 0288
2) desc = 8820
3) Difference = 8532
4) Repeating above steps as difference is not same
as n.
n = 8532
1) asc = 2358
2) desc = 8532
3) Difference = 6174
4) Repeating above steps as difference is not same
as n.
n = 6174
1) asc = 1467
2) desc = 7641
3) Difference = 6174
Stopping here as difference is same as n.