Number of ones in the smallest repunit
Last Updated :
05 May, 2021
Given a positive integer N whose unit’s digit is 3. Find the number of 1s in the smallest repunit which is divisible by the given number N. 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.
Examples:
Input: 3
Output: 3
As 3 divides 111 which is the smallest repunit
multiple of the number. So the number of ones in 111 is 3.
Input: 13
Output: 6
The repunits are 1, 11, 111, 1111, …. the next repunit to x will always be x*10+1. If the remainder left by x repunit is r then remainder left by the next repunit will always be (r*10+1)%n. Since the repunit can be very large, there is no need to find the repunit number. Simply counting the number of ones will give us the answer.
So, find out the remainders of all repunit numbers until the remainder becomes 0. Once it does, then the count of iterations done to make remainder 0 will be the number of 1’s.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int countOnes( int n)
{
int count = 1;
int rem = 1;
while (rem != 0) {
rem = (rem * 10 + 1) % n;
count++;
}
return count;
}
int main()
{
int n = 13;
cout << countOnes(n);
}
|
Java
import java.io.*;
class GFG
{
static int countOnes( int n)
{
int count = 1 ;
int rem = 1 ;
while (rem != 0 )
{
rem = (rem * 10 + 1 ) % n;
count++;
}
return count;
}
public static void main (String[] args)
{
int n = 13 ;
System.out.println(countOnes(n));
}
}
|
Python3
def countOnes(n):
count = 1 ;
rem = 1 ;
while (rem ! = 0 ):
rem = (rem * 10 + 1 ) % n;
count = count + 1 ;
return count;
n = 13 ;
print (countOnes(n));
|
C#
using System;
class GFG
{
static int countOnes( int n)
{
int count = 1;
int rem = 1;
while (rem != 0)
{
rem = (rem * 10 + 1) % n;
count++;
}
return count;
}
static public void Main ()
{
int n = 13;
Console.WriteLine (countOnes(n));
}
}
|
PHP
<?php
function countOnes( $n )
{
$count = 1;
$rem = 1;
while ( $rem != 0)
{
$rem = ( $rem * 10 + 1) % $n ;
$count ++;
}
return $count ;
}
$n = 13;
echo countOnes( $n );
?>
|
Javascript
<script>
function countOnes(n)
{
let count = 1;
let rem = 1;
while (rem != 0)
{
rem = (rem * 10 + 1) % n;
count++;
}
return count;
}
let n = 13;
document.write(countOnes(n));
</script>
|
Share your thoughts in the comments
Please Login to comment...