Generating numbers that are divisor of their right-rotations
Last Updated :
30 Aug, 2022
Given a number m, find all numbers which have m digits and are a divisor of their right rotation. Right-rotation of a number N is the result of rotating the digits of N one place to the right and wrapping the least significant digit around so that it becomes the most significant digit. For example, the right-rotation of 4356 is 6435.
Examples:
Input: 2
Output:
11
22
33
44
55
66
77
88
99
Input: 6
Output:
102564
111111
128205
142857
153846
179487
205128
222222
230769
333333
444444
555555
666666
777777
888888
999999
128205 satisfies the condition as 128205 * 4 = 512820.
Brute force approach:
The simplest approach is to traverse all the numbers which are greater than or equal to 10m-1 and less than 10m and check if they satisfy the required condition. We can check it in constant time, so the time complexity for the whole process is O(10m), which is feasible for only small values of m.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool rightRotationDivisor( int N)
{
int lastDigit = N % 10;
int rightRotation = (lastDigit * pow (10 , int ( log10 (N))))
+ floor (N / 10);
return (rightRotation % N == 0);
}
void generateNumbers( int m)
{
for ( int i= pow (10,(m - 1));i< pow (10 , m);i++)
if (rightRotationDivisor(i))
cout<<i<<endl;
}
int main()
{
int m = 3;
generateNumbers(m);
}
|
Java
public class GFG {
static boolean rightRotationDivisor( int N)
{
int lastDigit = N % 10 ;
int rightRotation = ( int )(lastDigit * Math.pow( 10 ,( int )(Math.log10(N)))
+ Math.floor(N / 10 ));
return (rightRotation % N == 0 );
}
static void generateNumbers( int m)
{
for ( int i= ( int )Math.pow( 10 ,(m - 1 )); i < Math.pow( 10 , m);i++)
if (rightRotationDivisor(i))
System.out.println(i);
}
public static void main(String args[])
{
int m = 3 ;
generateNumbers(m);
}
}
|
Python3
from math import log10
def rightRotationDivisor(N):
lastDigit = N % 10
rightRotation = (lastDigit * 10 * * int (log10(N))
+ N / / 10 )
return rightRotation % N = = 0
def generateNumbers(m):
for i in range ( 10 * * (m - 1 ), 10 * * m):
if rightRotationDivisor(i):
print (i)
m = 3
generateNumbers(m)
|
C#
using System;
public class GFG{
static bool rightRotationDivisor( int N)
{
int lastDigit = N % 10;
int rightRotation = ( int )(lastDigit * Math.Pow(10 ,( int )(Math.Log10(N)))
+ Math.Floor(( double )N/10));
return (rightRotation % N == 0);
}
static void generateNumbers( int m)
{
for ( int i= ( int )Math.Pow(10,(m - 1)); i < Math.Pow(10 , m);i++)
if (rightRotationDivisor(i))
Console.WriteLine(i);
}
public static void Main()
{
int m = 3;
generateNumbers(m);
}
}
|
PHP
<?php
function rightRotationDivisor( $N )
{
$lastDigit = $N % 10;
$rightRotation = ( $lastDigit * pow(10 ,
(int)(log10( $N )))) +
floor ( $N / 10);
return ( $rightRotation % $N == 0);
}
function generateNumbers( $m )
{
for ( $i = pow(10, ( $m - 1));
$i < pow(10 , $m ); $i ++)
if (rightRotationDivisor( $i ))
echo $i . "\n" ;
}
$m = 3;
generateNumbers( $m );
?>
|
Javascript
<script>
function rightRotationDivisor(N)
{
let lastDigit = N % 10;
let rightRotation = (lastDigit * Math.pow(10 ,
Math.floor((Math.log10(N)))) + Math.floor(N / 10));
return (rightRotation % N == 0);
}
function generateNumbers(m)
{
for (let i= Math.floor(Math.pow(10,(m - 1))); i <
Math.floor(Math.pow(10 , m));i++)
if (rightRotationDivisor(i))
document.write(i+ "<br>" );
}
let m = 3;
generateNumbers(m);
</script>
|
Output: 111
222
333
444
555
666
777
888
999
Time complexity: O(10m)
Auxiliary Space: O(1)
Efficient approach:
Let dmdm-1..d3d2d1 be the general form of the numbers that we want to generate. Take x = dmdm-1..d3d2 and y = d1. The condition that we want to satisfy is y * 10m – 1 + x = k * (10x + y) where k is a positive integer. Rearranging the terms, we get x = (y * (10m-1 – k)) / (10k – 1). Thus, if we fix the value of y and k, we can get a value of x such that 10x + y is a number we need to generate.
The value of y can range from 1 to 9; observe that we will not have the case y = 0 as that would make the right rotation y * 10m – 1 + x have m – 1 digit, and the required condition will never be met.
We require x to have exactly m – 1 digit, i.e.
We can observe that the lower bound is always less than unity, so we can keep it at 1 since k has to be a positive integer.
We can use these results to obtain a highly efficient solution that runs with constant time complexity, i.e. O(1). An important point to note is that the numbers obtained may not be in a sorted form, so we need to store and explicitly sort them if we wish to obtain the numbers in a sorted fashion.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generateNumbers( int m)
{
vector< int > numbers;
int k_max, x;
for ( int y = 0; y < 10; y++)
{
k_max = ( int )( pow (10, m - 2) *
(10 * y + 1)) /
( int )( pow (10, m - 1) + y);
for ( int k = 1; k <= k_max; k++)
{
x = ( int )(y * ( pow (10, m - 1) - k)) /
(10 * k - 1);
if (( int )(y * ( pow (10, m - 1) - k)) %
(10 * k - 1) == 0)
numbers.push_back(10 * x + y);
}
}
sort(numbers.begin(), numbers.end());
for ( int i = 0; i < numbers.size(); i++)
cout << (numbers[i]) << endl;
}
int main()
{
int m = 3;
generateNumbers(m);
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static void generateNumbers( int m)
{
ArrayList<Integer> numbers = new ArrayList<>();
int k_max, x;
for ( int y = 0 ; y < 10 ; y++)
{
k_max = ( int )(Math.pow( 10 ,m- 2 ) * ( 10 * y + 1 )) /
( int )(Math.pow( 10 , m- 1 ) + y);
for ( int k = 1 ; k <= k_max; k++)
{
x = ( int )(y*(Math.pow( 10 ,m- 1 )-k)) / ( 10 * k - 1 );
if (( int )(y*(Math.pow( 10 ,m- 1 )-k)) % ( 10 * k - 1 ) == 0 )
numbers.add( 10 * x + y);
}
}
Collections.sort(numbers);
for ( int i = 0 ; i < numbers.size(); i++)
System.out.println(numbers.get(i));
}
public static void main(String args[])
{
int m = 3 ;
generateNumbers(m);
}
}
|
Python 3
from math import log10
def generateNumbers(m):
numbers = []
for y in range ( 1 , 10 ):
k_max = (( 10 * * (m - 2 ) *
( 10 * y + 1 )) / /
( 10 * * (m - 1 ) + y))
for k in range ( 1 , k_max + 1 ):
x = ((y * ( 10 * * (m - 1 ) - k))
/ / ( 10 * k - 1 ))
if ((y * ( 10 * * (m - 1 ) - k))
% ( 10 * k - 1 ) = = 0 ):
numbers.append( 10 * x + y)
for n in sorted (numbers):
print (n)
m = 3
generateNumbers(m)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void generateNumbers( int m)
{
List< int > numbers = new List< int >();
int k_max, x;
for ( int y = 0; y < 10; y++)
{
k_max = ( int )(Math.Pow(10, m - 2) * (10 * y + 1)) /
( int )(Math.Pow(10, m - 1) + y);
for ( int k = 1; k <= k_max; k++)
{
x = ( int )(y * (Math.Pow(10, m - 1) - k)) /
(10 * k - 1);
if (( int )(y * (Math.Pow(10, m - 1) - k)) %
(10 * k - 1) == 0)
numbers.Add(10 * x + y);
}
}
numbers.Sort();
for ( int i = 0; i < numbers.Count; i++)
Console.WriteLine(numbers[i]);
}
public static void Main(String []args)
{
int m = 3;
generateNumbers(m);
}
}
|
Javascript
<script>
function generateNumbers(m)
{
let numbers = [];
let k_max, x;
for (let y = 0; y < 10; y++)
{
k_max = Math.floor((Math.pow(10,m-2) * (10 * y + 1)) /
Math.floor(Math.pow(10, m-1) + y));
for (let k = 1; k <= k_max; k++)
{
x = Math.floor((y*(Math.pow(10,m-1)-k)) / (10 * k -1));
if (Math.floor((y*(Math.pow(10,m-1)-k)) % (10 * k -1)) == 0)
numbers.push(10 * x + y);
}
}
numbers.sort( function (a,b){ return a-b;});
for (let i = 0; i < numbers.length; i++)
document.write(numbers[i]+ "<br>" );
}
let m = 3;
generateNumbers(m);
</script>
|
Output: 111
222
333
444
555
666
777
888
999
Time complexity: O(nlogn)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...