给定正整数n,以十进制值1 / n查找周期。以十进制值表示的期间是不断重复的位数(小数点后的某处)。
例子 :
Input: n = 3
Output: 1
The value of 1/3 is 0.333333...
Input: n = 7
Output: 6
The value of 1/7 is 0.142857142857142857.....
Input: n = 210
Output: 6
The value of 1/210 is 0.0047619047619048.....
让我们首先讨论一个简单的问题,即找到值为1 / n的单个数字。
如何找到1 / n值的单个数字?
让我们以一个例子来了解该过程。例如,对于n =7。可以通过执行10/7获得第一个数字。第二个数字可以通过30/7来获得(上一个除法运算中的余数为3)。第三位数字可以通过20/7获得(2是上一个除法的余数)。因此,想法是获取第一个数字,然后继续将(remainder * 10)/ n的值用作下一个数字,并保持更新余数为(remainder * 10)%10。此处讨论了完整的程序。
如何找到时期?
1 / n的周期等于上述过程中使用的余数的周期。从数字直接取自余数的事实可以很容易地证明这一点。
关于余数序列的一个有趣的事实是,该余数序列周期中的所有燕鸥都是截然不同的。这样做的原因很简单,如果剩余部分重复出现,那就是新时期的开始。
以下是上述想法的实现。
CPP
// C++ program to find length of period of 1/n
#include
#include
C++
// C++ program to find length of period of 1/n without
// using map or hash
#include
using namespace std;
// Function to find length of period in 1/n
int getPeriod(int n)
{
// Find the (n+1)th remainder after decimal point
// in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n+1; i++)
rem = (10*rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10*rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver program to test above function
int main()
{
cout << getPeriod(3) << endl;
cout << getPeriod(7) << endl;
return 0;
}
Java
// Java program to find length
// of period of 1/n without using
// map or hash
class GFG {
// Function to find length of period in 1/n
static int getPeriod(int n)
{
// Find the (n+1)th remainder after
// decimal point in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n + 1; i++)
rem = (10 * rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10 * rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver code
public static void main(String[] args)
{
System.out.println(getPeriod(3) );
System.out.println(getPeriod(7));
}
}
// This code is contributed by Smitha Dinesh Semwal
Python3
# Python3 program to find length of
# period of 1/n without using map or hash
# Function to find length
# of period in 1/n
def getPeriod( n) :
# Find the (n+1)th remainder after
# decimal point in value of 1/n
rem = 1 # Initialize remainder
for i in range(1, n + 2):
rem = (10 * rem) % n
# Store (n+1)th remainder
d = rem
# Count the number of remainders
# before next occurrence of
# (n+1)'th remainder 'd'
count = 0
rem = (10 * rem) % n
count += 1
while rem != d :
rem = (10 * rem) % n
count += 1
return count
# Driver Code
if __name__ == "__main__":
print (getPeriod(3))
print (getPeriod(7))
# This code is contributed
# by ChitraNayal
C#
// C# program to find length of
// period of 1/n without using
// map or hash
using System;
class GFG {
// Function to find length of period in 1/n
static int getPeriod(int n)
{
// Find the (n+1)th remainder after
// decimal point in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n + 1; i++)
rem = (10 * rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10 * rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver code
public static void Main()
{
Console.Write(getPeriod(3) + "\n");
Console.Write(getPeriod(7));
}
}
// This code is contributed by Smitha Dinesh Semwal
PHP
Javascript
输出:
1
6
利用以下事实,我们可以避免使用map或hash。对于数字n,最多可以有n个不同的余数。同样,该期间可能不是从第一个余数开始的,因为一些初始余数可能是非重复的(不是任何时期的一部分)。因此,要确保从一个周期中选取了余数,请从第(n + 1)个余数开始并继续寻找下一个余数。第(n + 1)次余数与下一次出现之间的距离是周期的长度。
C++
// C++ program to find length of period of 1/n without
// using map or hash
#include
using namespace std;
// Function to find length of period in 1/n
int getPeriod(int n)
{
// Find the (n+1)th remainder after decimal point
// in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n+1; i++)
rem = (10*rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10*rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver program to test above function
int main()
{
cout << getPeriod(3) << endl;
cout << getPeriod(7) << endl;
return 0;
}
Java
// Java program to find length
// of period of 1/n without using
// map or hash
class GFG {
// Function to find length of period in 1/n
static int getPeriod(int n)
{
// Find the (n+1)th remainder after
// decimal point in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n + 1; i++)
rem = (10 * rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10 * rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver code
public static void main(String[] args)
{
System.out.println(getPeriod(3) );
System.out.println(getPeriod(7));
}
}
// This code is contributed by Smitha Dinesh Semwal
Python3
# Python3 program to find length of
# period of 1/n without using map or hash
# Function to find length
# of period in 1/n
def getPeriod( n) :
# Find the (n+1)th remainder after
# decimal point in value of 1/n
rem = 1 # Initialize remainder
for i in range(1, n + 2):
rem = (10 * rem) % n
# Store (n+1)th remainder
d = rem
# Count the number of remainders
# before next occurrence of
# (n+1)'th remainder 'd'
count = 0
rem = (10 * rem) % n
count += 1
while rem != d :
rem = (10 * rem) % n
count += 1
return count
# Driver Code
if __name__ == "__main__":
print (getPeriod(3))
print (getPeriod(7))
# This code is contributed
# by ChitraNayal
C#
// C# program to find length of
// period of 1/n without using
// map or hash
using System;
class GFG {
// Function to find length of period in 1/n
static int getPeriod(int n)
{
// Find the (n+1)th remainder after
// decimal point in value of 1/n
int rem = 1; // Initialize remainder
for (int i = 1; i <= n + 1; i++)
rem = (10 * rem) % n;
// Store (n+1)th remainder
int d = rem;
// Count the number of remainders before next
// occurrence of (n+1)'th remainder 'd'
int count = 0;
do {
rem = (10 * rem) % n;
count++;
} while(rem != d);
return count;
}
// Driver code
public static void Main()
{
Console.Write(getPeriod(3) + "\n");
Console.Write(getPeriod(7));
}
}
// This code is contributed by Smitha Dinesh Semwal
的PHP
Java脚本
输出:
1
6