Number of 1's in smallest repunits
PROBLEM :
Every number whose unit’s digit is 3 has a repunit as its multiple. A repunit is a number which has only ones. It is of the form (10n – 1)/9. Example: 3 divides 111, 13 divides 111111.
A positive integer N will be given whose unit’s digit is 3. The task is to find the number of 1s in the smallest repunit which is divisible by the given number N.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line containing a positive integer N whose unit’s digit is 3.
Output:
Corresponding to each test case, in a new line, print the number of 1s in the smallest repunit multiple of the number.
Constraints:
1 = T = 100
1 = N = 107
Example:
Input
2
3
23
Output
3
22
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long smallest_repunits(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<smallest_repunits(no)<<endl ;
}
return 0;
}
long smallest_repunits(long long no)
{
int rem ;
long count ;
rem=1 ;
count=1 ;
while(rem)
{
rem=(rem*10+1)%no ;
count++ ;
}
return count ;
}
---------------------------------------------------------------------------------
Every number whose unit’s digit is 3 has a repunit as its multiple. A repunit is a number which has only ones. It is of the form (10n – 1)/9. Example: 3 divides 111, 13 divides 111111.
A positive integer N will be given whose unit’s digit is 3. The task is to find the number of 1s in the smallest repunit which is divisible by the given number N.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line containing a positive integer N whose unit’s digit is 3.
Output:
Corresponding to each test case, in a new line, print the number of 1s in the smallest repunit multiple of the number.
Constraints:
1 = T = 100
1 = N = 107
Example:
Input
2
3
23
Output
3
22
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long smallest_repunits(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<smallest_repunits(no)<<endl ;
}
return 0;
}
long smallest_repunits(long long no)
{
int rem ;
long count ;
rem=1 ;
count=1 ;
while(rem)
{
rem=(rem*10+1)%no ;
count++ ;
}
return count ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment