📜  循环数

📅  最后修改于: 2021-04-29 03:40:19             🧑  作者: Mango

循环数是整数,其中数字的循环排列是该数的连续倍数。最广为人知的是六位数字142857(请参见下面示例中的说明)。

以下平凡的情况通常不包括在循环数中。

  • 一位数字,例如:5
  • 重复数字,例如:555
  • 重复的循环数,例如:142857142857

给定一个数字,检查它是否循环。

例子:

Input : 142857
Output : Yes
Explanation
    142857 × 1 = 142857
    142857 × 2 = 285714
    142857 × 3 = 428571
    142857 × 4 = 571428
    142857 × 5 = 714285
    142857 × 6 = 857142 

我们生成该数字的所有循环排列,并检查每个排列是否将数字除以not。我们还将检查三个条件。如果三个条件中的任何一个为true,则返回false。

C++
// Program to check if a number is cyclic.
#include 
using namespace std;
  
#define ull unsigned long long int
  
// Function to generate all cyclic permutations
// of a number
bool isCyclic(ull N)
{
    // Count digits and check if all
    // digits are same
    ull num = N;
    int count = 0;
    int digit = num % 10;
    bool allSame = true;
    while (num) {
        count++;
        if (num % 10 != digit)
            allSame = false;
        num = num / 10;
    }
  
    // If all digits are same, then
    // not considered cyclic.
    if (allSame == true)
        return false;
  
    // If counts of digits is even and
    // two halves are same, then the
    // number is not considered cyclic.
    if (count % 2 == 0) {
        ull halfPower = pow(10, count / 2);
        ull firstHalf = N % halfPower;
        ull secondHalf = N / halfPower;
        if (firstHalf == firstHalf && isCyclic(firstHalf))
            return false;
    }
  
    num = N;
    while (1) {
  
        // Following three lines generates a
        // circular pirmutation of a number.
        ull rem = num % 10;
        ull div = num / 10;
        num = (pow(10, count - 1)) * rem + div;
  
        // If all the permutations are checked
        // and we obtain original number exit
        // from loop.
        if (num == N)
            break;
  
        if (num % N != 0)
            return false;
    }
  
    return true;
}
  
// Driver Program
int main()
{
    ull N = 142857;
    if (isCyclic(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java Program to check if a number is cyclic
  
class GFG {
    // Function to generate all cyclic
    // permutations of a number
    static boolean isCyclic(long N)
    {
        // Count digits and check if all
        // digits are same
        long num = N;
        int count = 0;
        int digit = (int)(num % 10);
        boolean allSame = true;
        while (num > 0) {
            count++;
            if (num % 10 != digit)
                allSame = false;
            num = num / 10;
        }
  
        // If all digits are same, then
        // not considered cyclic.
        if (allSame == true)
            return false;
  
        // If counts of digits is even and
        // two halves are same, then the
        // number is not considered cyclic.
        if (count % 2 == 0) {
            long halfPower = (long)Math.pow(10, count / 2);
            long firstHalf = N % halfPower;
            long secondHalf = N / halfPower;
            if (firstHalf == firstHalf && isCyclic(firstHalf))
                return false;
        }
  
        num = N;
        while (true) {
            // Following three lines generates a
            // circular pirmutation of a number.
            long rem = num % 10;
            long div = num / 10;
            num = (long)(Math.pow(10, count - 1))
                      * rem
                  + div;
  
            // If all the permutations are checked
            // and we obtain original number exit
            // from loop.
            if (num == N)
                break;
  
            if (num % N != 0)
                return false;
        }
  
        return true;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        long N = 142857;
        if (isCyclic(N))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Program to check if
# a number is cyclic
# Function to generate
# all cyclic permutations
# of a number
def isCyclic(N):
  
    # Count digits and check if all
    # digits are same
    num = N
    count = 0
    digit =(num % 10)
    allSame = True
  
    while (num>0):
        count+= 1
        if (num % 10 != digit):
            allSame = False
        num = num // 10
      
   
    # If all digits are same, then
    # not considered cyclic.
    if (allSame == True):
        return False
   
    # If counts of digits is even and
    # two halves are same, then the
    # number is not considered cyclic.
    if (count % 2 == 0):
      
        halfPower = pow(10, count//2)
        firstHalf = N % halfPower
        secondHalf = N / halfPower
        if (firstHalf == firstHalf and
            isCyclic(firstHalf)):
            return False
      
   
    num = N
    while (True):
   
        # Following three lines
        # generates a
        # circular pirmutation
        # of a number.
        rem = num % 10
        div = num // 10
        num = pow(10, count - 1) * rem + div
   
        # If all the permutations
        # are checked
        # and we obtain original
        # number exit
        # from loop.
        if (num == N):
            break
   
        if (num % N != 0):
            return False
      
    return True
  
# Driver code
  
N = 142857
if (isCyclic(N)):
    print("Yes")
else:
    print("No")
  
# This code is contributed
# by Anant Agarwal.


C#
// C# Program to check if a number is cyclic
using System;
  
class GFG {
      
    // Function to generate all cyclic
    // permutations of a number
    static bool isCyclic(long N)
    {
          
        // Count digits and check if all
        // digits are same
        long num = N;
        int count = 0;
        int digit = (int)(num % 10);
        bool allSame = true;
        while (num > 0)
        {
            count++;
            if (num % 10 != digit)
                allSame = false;
            num = num / 10;
        }
  
        // If all digits are same, then
        // not considered cyclic.
        if (allSame == true)
            return false;
  
        // If counts of digits is even and
        // two halves are same, then the
        // number is not considered cyclic.
        if (count % 2 == 0) {
            long halfPower = (long)Math.Pow(10,
                                    count / 2);
                                      
            long firstHalf = N % halfPower;
              
            // long secondHalf = N / halfPower;
            if (firstHalf == firstHalf && 
                           isCyclic(firstHalf))
                return false;
        }
  
        num = N;
        while (true)
        {
              
            // Following three lines generates a
            // circular pirmutation of a number.
            long rem = num % 10;
            long div = num / 10;
            num = (long)(Math.Pow(10, count - 1))
                    * rem + div;
  
            // If all the permutations are checked
            // and we obtain original number exit
            // from loop.
            if (num == N)
                break;
  
            if (num % N != 0)
                return false;
        }
  
        return true;
    }
  
    // Driver code
    public static void Main()
    {
        long N = 142857;
          
        if (isCyclic(N))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.


PHP
 0)
    {
        $count += 1;
        if ($num % 10 != $digit)
        $allSame = false;
        $num = (int)($num / 10);
    }
  
    // If all digits are same, then
    // not considered cyclic.
    if ($allSame == true)
        return false;
  
    // If counts of digits is even and
    // two halves are same, then the
    // number is not considered cyclic.
    if ($count % 2 == 0)
    {
        $halfPower = pow(10, (int)($count / 2));
        $firstHalf = $N % $halfPower;
        $secondHalf = $N / $halfPower;
        if ($firstHalf == $firstHalf && 
                 isCyclic($firstHalf))
            return false;
    }
  
    $num = $N;
    while (true)
    {
  
        // Following three lines generates a
        // circular pirmutation of a number.
        $rem = $num % 10;
        $div = (int)($num / 10);
        $num = pow(10, $count - 1) * $rem + $div;
  
        // If all the permutations are checked
        // and we obtain original number, exit
        // from loop.
        if ($num == $N)
            break;
  
        if ($num % $N != 0)
            return false;
    }
    return true;
}
  
// Driver code
$N = 142857;
if (isCyclic($N))
    print("Yes");
else
    print("No");
  
// This code is contributed by mits
?>


输出:

Yes

参考 :
https://zh.wikipedia.org/wiki/循环编号