Dado un entero positivo N cuyo dígito unitario es 3. Encuentra el número de 1 en la repunidad más pequeña que es divisible por el número dado N. Cada número cuyo dígito unitario es 3 tiene un repunit como su múltiplo. Un repunit es un número que tiene sólo unos. Es de la forma (10n – 1)/9.
Ejemplos:
Entrada: 3
Salida: 3
Como 3 divide 111 que es el
múltiplo de repunidad más pequeño del número. Entonces el número de unos en 111 es 3.
Entrada: 13
Salida: 6
Las repeticiones son 1, 11, 111, 1111, …. la siguiente repunidad a x siempre será x*10+1. Si el resto que deja x repetición es r, entonces el resto que deja la siguiente repetición siempre será (r*10+1)%n. Dado que la repunit puede ser muy grande, no es necesario encontrar el número de repunit. Simplemente contar el número de unos nos dará la respuesta.
Por lo tanto, averigüe los restos de todos los números de repunit hasta que el resto se convierta en 0. Una vez que lo haga, el recuento de iteraciones realizadas para hacer que el resto sea 0 será el número de 1.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to print the number of 1s in // smallest repunit multiple of the number. #include <bits/stdc++.h> using namespace std; // Function to find number of 1s in // smallest repunit multiple of the number int countOnes(int n) { // to store number of 1s in smallest // repunit multiple of the number. int count = 1; // initialize rem with 1 int rem = 1; // run loop until rem becomes zero while (rem != 0) { // rem*10 + 1 here represents // the repunit modulo n rem = (rem * 10 + 1) % n; count++; } // when remainder becomes 0 // return count return count; } // Driver Code int main() { int n = 13; // Calling function cout << countOnes(n); }
Java
// Java program to print the // number of 1s in smallest // repunit multiple of the number. import java.io.*; class GFG { // Function to find number // of 1s in smallest repunit // multiple of the number static int countOnes(int n) { // to store number of 1s // in smallest repunit // multiple of the number. int count = 1; // initialize rem with 1 int rem = 1; // run loop until // rem becomes zero while (rem != 0) { // rem*10 + 1 here // represents the // repunit modulo n rem = (rem * 10 + 1) % n; count++; } // when remainder becomes // 0 return count return count; } // Driver Code public static void main (String[] args) { int n = 13; // Calling function System.out.println(countOnes(n)); } } // This code is contributed by akt_mit
Python3
# Python3 program to print the # number of 1s in smallest # repunit multiple of the number. # Function to find number # of 1s in smallest repunit # multiple of the number def countOnes(n): # to store number of 1s # in smallest repunit # multiple of the number. count = 1; # initialize rem with 1 rem = 1; # run loop until # rem becomes zero while (rem != 0): # rem*10 + 1 here represents # the repunit modulo n rem = (rem * 10 + 1) % n; count = count + 1; # when remainder becomes # 0 return count return count; # Driver Code n = 13; # Calling function print(countOnes(n)); # This code is contributed by mits
C#
// C# program to print the // number of 1s in smallest // repunit multiple of the number. using System; class GFG { // Function to find number // of 1s in smallest repunit // multiple of the number static int countOnes(int n) { // to store number of 1s // in smallest repunit // multiple of the number. int count = 1; // initialize rem with 1 int rem = 1; // run loop until // rem becomes zero while (rem != 0) { // rem*10 + 1 here represents // the repunit modulo n rem = (rem * 10 + 1) % n; count++; } // when remainder becomes 0 // return count return count; } // Driver Code static public void Main () { int n = 13; // Calling function Console.WriteLine (countOnes(n)); } } // This code is contributed by ajit
PHP
<?php // PHP program to print the // number of 1s in smallest // repunit multiple of the number. // Function to find number // of 1s in smallest repunit // multiple of the number function countOnes($n) { // to store number of 1s // in smallest repunit // multiple of the number. $count = 1; // initialize rem with 1 $rem = 1; // run loop until // rem becomes zero while ($rem != 0) { // rem*10 + 1 here represents // the repunit modulo n $rem = ($rem * 10 + 1) % $n; $count++; } // when remainder becomes // 0 return count return $count; } // Driver Code $n = 13; // Calling function echo countOnes($n); // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to print the // number of 1s in smallest // repunit multiple of the number. // Function to find number // of 1s in smallest repunit // multiple of the number function countOnes(n) { // to store number of 1s // in smallest repunit // multiple of the number. let count = 1; // initialize rem with 1 let rem = 1; // run loop until // rem becomes zero while (rem != 0) { // rem*10 + 1 here represents // the repunit modulo n rem = (rem * 10 + 1) % n; count++; } // when remainder becomes 0 // return count return count; } let n = 13; // Calling function document.write(countOnes(n)); // This code is contributed by rameshtravel07. </script>
6
Publicación traducida automáticamente
Artículo escrito por akshitSingh47 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA