Cyclic Number

A cyclic number is an integer in which cyclic permutations of the digits are successive multiples of the number. The most widely known is the six-digit number 142857, whose first six integer multiples are
now let see mathematically the formula to check cyclic number.
The following trivial cases are typically excluded:

  • Single digits, e.g.: 5
  • Repeated digits, e.g.: 555
  • Repeated cyclic numbers, e.g.: 142857142857

Given a number, check if it is cyclic or not.

Examples:

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

We generate all cyclic permutations of the number and check if every permutation divides number of not. We also check for three conditions. If any of the three conditions is true, we return false.

// Program to check if a number is cyclic.
#include <bits/stdc++.h>
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;
}

Output:

Yes

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar