如果数字可以用大于等于2的基数表示为三个或更多1的字符串,则它是基数B中的一个Repunit。
检查N是否为中继单元号
给定一个整数N,任务是检查N是否为基数B中的Repunit数。
例子:
Input: N = 31, B = 5
Output: Yes
31 can be written as 111 base in 5
Input: N = 5, B = 2
Output: No
5 is 101 in base 2
方法:我们将计算一个人的数量在给定的数N的基B和也算在一个给定的数N的基B的位数。如果它们相同,则打印“是”,否则打印“否”。
例如:
N = 31, B = 5
31 can be written as 111 base in 5, So number of one’s in base B of a given number N = 3 and number of digits in the base B of a given number N = 3
Since both are equal hence 31 is a Repunit number in base 5.
下面是上述方法的实现:
C++
// C++ implementation to check
// if a number is Repunit Number
#include
#include
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
输出:
Yes
参考:http://www.numbersaplenty.com/set/repunit/