Un número es una Repunit en base B si se puede representar como una string de tres o más unos en base >= 2.
Comprobar si N es un número de Repunit
Dado un número entero N, la tarea es comprobar si N es un número de Repunit en base B.
Ejemplos:
Entrada: N = 31, B = 5
Salida: Sí
31 se puede escribir como 111 en base 5
Entrada: N = 5, B = 2
Salida: No
5 es 101 en base 2
Enfoque: Contaremos el número de unos en la base B de un número N dado y también contaremos el número de dígitos en la base B de un número N dado . Si son iguales, escriba «SÍ», de lo contrario, escriba «NO».
Por ejemplo:
N = 31, B = 5
31 se puede escribir como 111 en base 5, por lo que el número de unos en base B de un número dado N = 3 y el número de dígitos en la base B de un número dado N = 3
Dado que ambos son iguales por lo tanto, 31 es un número Repunit en base 5 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check // if a number is Repunit Number #include <iostream> #include <math.h> using namespace std; // Function to check if a number // contains all the digits 0, 1, .., (b-1) // an equal number of times bool isRepunitNum(int n, int b) { // to store number of digits of n // in base B int length = 0; // to count frequency of digit 1 int countOne = 0; while (n != 0) { int r = n % b; length++; if (r == 1) countOne++; n = n / b; } // condition to check three or more 1's // and number of ones is equal to number // of digits of n in base B return countOne >= 3 && countOne == length; } // Driver Code int main() { // taking inputs int n = 31; int base = 2; // function to check if (isRepunitNum(n, base)) cout << "Yes"; else cout << "NO"; return 0; }
Java
// Java implementation to check // if a number is Repunit Number class GFG{ // Function to check if a number // contains all the digits 0, 1, .., (b-1) // an equal number of times static boolean isRepunitNum(int n, int b) { // to store number of digits of n // in base B int length = 0; // to count frequency of digit 1 int countOne = 0; while (n != 0) { int r = n % b; length++; if (r == 1) countOne++; n = n / b; } // condition to check three or more 1's // and number of ones is equal to number // of digits of n in base B return countOne >= 3 && countOne == length; } // Driver Code public static void main(String[] args) { // taking inputs int n = 31; int base = 2; // function to check if (isRepunitNum(n, base)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by rock_cool
Python3
# Python3 implementation to check # if a number is Repunit Number # Function to check if a number # contains all the digits 0, 1, .., (b-1) # an equal number of times def isRepunitNum(n, b): # to store number of digits of n # in base B length = 0; # to count frequency of digit 1 countOne = 0; while (n != 0): r = n % b; length += 1; if (r == 1): countOne += 1; n = n // b; # condition to check three or more 1's # and number of ones is equal to number # of digits of n in base B return countOne >= 3 and countOne == length; # Driver Code if __name__ == '__main__': # taking inputs n = 31; base = 2; # function to check if (isRepunitNum(n, base)): print("Yes"); else: print("No"); # This code is contributed by 29AjayKumar
C#
// C# implementation to check // if a number is Repunit Number using System; class GFG{ // Function to check if a number // contains all the digits 0, 1, .., (b-1) // an equal number of times static bool isRepunitNum(int n, int b) { // to store number of digits of n // in base B int length = 0; // to count frequency of digit 1 int countOne = 0; while (n != 0) { int r = n % b; length++; if (r == 1) countOne++; n = n / b; } // condition to check three or more 1's // and number of ones is equal to number // of digits of n in base B return countOne >= 3 && countOne == length; } // Driver Code public static void Main() { // taking inputs int n = 31; int base1 = 2; // function to check if (isRepunitNum(n, base1)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript implementation to check // if a number is Repunit Number // Function to check if a number // contains all the digits 0, 1, .., (b-1) // an equal number of times function isRepunitNum( n ,b) { // to store number of digits of n // in base B let length = 0; // to count frequency of digit 1 let countOne = 0; while (n != 0) { let r = n % b; length++; if (r == 1) countOne++; n = parseInt(n / b); } // condition to check three or more 1's // and number of ones is equal to number // of digits of n in base B return countOne >= 3 && countOne == length; } // Driver Code // taking inputs let n = 31; let base = 2; // function to check if (isRepunitNum(n, base)) document.write("Yes"); else document.write("No"); // This code contributed by gauravrajput1 </script>
Yes
Complejidad del tiempo: O(log b n)
Espacio Auxiliar: O(1)
Referencia : http://www.numbersaplenty.com/set/repunit/