📜  最小repunit中的个数

📅  最后修改于: 2021-05-07 01:47:50             🧑  作者: Mango

给定一个正整数N,其单位数字为3。在最小重复单位中找到1的数目,该最小单位可被给定数字N整除。每个单位位数为3的数字都有一个重复单位作为其倍数。 repunit是一个只有一个的数字。它的形式为(10n – 1)/ 9。

例子:

repunits是1,11,111,1111,…。 x的下一个重复单位将始终为x * 10 + 1。如果x repunit剩余的余数为r,则下一个repunit剩余的余数将始终为(r * 10 + 1)%n。由于repunit可能非常大,因此无需查找repunit编号。只需数一数即可得到答案。
因此,找出所有重复单元数的余数,直到余数变为0。一旦取整,则使余数为0的迭代次数将为1。

下面是上述方法的实现:

C++
// CPP program to print the number of 1s in
// smallest repunit multiple of the number.
#include 
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


输出:
6