Given a positive integer n, find first k digits after the decimal in the value of 1/n and return it as a string. Your program should avoid overflow and floating-point arithmetic.
Examples :
Input: n = 3, k = 3
Output: "333"
Explanation: 1/3 = 0.33333, so after a point, 3 digits are 3, 3 and 3.
Input: n = 50, k = 4
Output: "0200"
Explanation: 1/50 = 0.020000, so after a point, 4 digits are 0, 2, 0 and 0.
Table of Content
[Naive Approach] Long Division with Repeated Subtraction - O(k) Time and O(k) Space
The idea is to simulate the long division process without using the division (/) and modulus (%) operators. In each iteration, multiply the remainder by 10 and repeatedly subtract
nfrom it. The number of subtractions gives the next digit after the decimal point, and the remaining value becomes the new remainder. Repeat this processktimes to generate the firstkdigits.
Let us understand with example:
Input: n = 50, k = 4
- Initially, rem = 1 and res = "".
- Iteration 1: rem = 1 × 10 = 10. Since 10 < 50, no subtraction is performed, so digit = 0. Append '0' to res, giving res = "0". The remainder remains 10.
- Iteration 2: rem = 10 × 10 = 100. Subtract 50 twice: 100 -> 50 -> 0. Thus, digit = 2 and rem = 0. Append '2', so res = "02".
- Iteration 3: rem = 0 × 10 = 0. Since 0 < 50, digit = 0. Append '0', so res = "020". The remainder remains 0.
- Iteration 4: rem = 0 × 10 = 0. Again, no subtraction is performed, so digit = 0. Append '0', giving res = "0200".
Thus, the first 4 digits after the decimal point in 1/50 = 0.020000... are "0200".
#include <iostream>
using namespace std;
string Kdigits(int n, int k)
{
// Stores the resultant digits
string res = "";
// Initialize remainder
int rem = 1;
// Generate k digits
for (int i = 0; i < k; i++)
{
rem *= 10;
// Count how many times n can be subtracted
int digit = 0;
while (rem >= n)
{
rem -= n;
digit++;
}
res += char(digit + '0');
}
return res;
}
int main()
{
int n = 50, k = 4;
cout << Kdigits(n, k);
return 0;
}
public class GFG {
static String Kdigits(int n, int k)
{
String res = "";
// Initialize remainder
int rem = 1;
// Generate k digits
for (int i = 0; i < k; i++) {
rem *= 10;
// Count how many times n can be subtracted
int digit = 0;
while (rem >= n) {
rem -= n;
digit++;
}
res += (char)(digit + '0');
}
return res;
}
public static void main(String[] args)
{
int n = 50, k = 4;
System.out.println(Kdigits(n, k));
}
}
def Kdigits(n, k):
# Stores the resultant digits
res = ""
# Initialize remainder
rem = 1
# Generate k digits
for i in range(k):
rem *= 10
# Count how many times n can be subtracted
digit = 0
while rem >= n:
rem -= n
digit += 1
res += chr(digit + ord('0'))
return res
if __name__ == "__main__":
n = 50
k = 4
print(Kdigits(n, k))
using System;
class GFG
{
static string Kdigits(int n, int k)
{
// Stores the resultant digits
string res = "";
// Initialize remainder
int rem = 1;
// Generate k digits
for (int i = 0; i < k; i++)
{
rem *= 10;
// Count how many times n can be subtracted
int digit = 0;
while (rem >= n)
{
rem -= n;
digit++;
}
res += (char)(digit + '0');
}
return res;
}
static void Main()
{
int n = 50, k = 4;
Console.WriteLine(Kdigits(n, k));
}
}
function Kdigits(n, k)
{
let res = "";
// Initialize remainder
let rem = 1;
// Generate k digits
for (let i = 0; i < k; i++) {
rem *= 10;
// Count how many times n can be subtracted
let digit = 0;
while (rem >= n) {
rem -= n;
digit++;
}
res += String.fromCharCode(digit
+ "0".charCodeAt(0));
}
return res;
}
// Driver Code
let n = 50, k = 4;
console.log(Kdigits(n, k));
Output
0200
[Expected Approach] Long Division Using Remainder - O(k) Time and O(1) Space
The idea is to simulate the long division process used to find decimal digits. Start with remainder = 1 and repeatedly multiply it by 10. The next digit after the decimal point is obtained by (10 * remainder) / n and the new remainder becomes (10 * remainder) % n. Repeating this process k times generates the first k digits after the decimal point without using floating-point arithmetic.
Let us understand with example:
Input: n = 50, k = 4
- Initially, rem = 1 and res = "".
- Iteration 1: digit = (10 * 1) / 50 = 0, rem = (10 * 1) % 50 = 10, so res = "0".
- Iteration 2: digit = (10 * 10) / 50 = 2, rem = 0, so res = "02".
- Iteration 3: digit = 0, rem = 0, so res = "020".
- Iteration 4: digit = 0, rem = 0, so res = "0200".
Thus, the first 4 digits after the decimal point in 1/50 = 0.020000... are "0200".
#include <iostream>
using namespace std;
string Kdigits(int n, int k)
{
// Stores the resultant digits
string res = "";
// Initialize remainder
int rem = 1;
// Generate first k digits after decimal
for (int i = 0; i < k; i++)
{
// Obtain next digit
int digit = (10 * rem) / n;
res += char(digit + '0');
// Update remainder
rem = (10 * rem) % n;
}
return res;
}
int main()
{
int n = 50, k = 4;
cout << Kdigits(n, k);
return 0;
}
class GFG {
public static String Kdigits(int n, int k) {
// Stores the resultant digits
StringBuilder res = new StringBuilder();
// Initialize remainder
int rem = 1;
// Generate first k digits after decimal
for (int i = 0; i < k; i++) {
// Obtain next digit
int digit = (10 * rem) / n;
res.append(digit);
// Update remainder
rem = (10 * rem) % n;
}
return res.toString();
}
public static void main(String[] args)
{
int n = 50, k = 4;
System.out.println(Kdigits(n, k));
}
}
def Kdigits(n, k):
# Stores the resultant digits
res = ""
# Initialize remainder
rem = 1
# Generate first k digits after decimal
for i in range(k):
# Obtain next digit
digit = (10 * rem) // n
res += str(digit)
# Update remainder
rem = (10 * rem) % n
return res
if __name__ == "__main__":
n = 50
k = 4
print(Kdigits(n, k))
using System;
using System.Text;
class GFG {
public string Kdigits(int n, int k)
{
// Stores the resultant digits
StringBuilder res = new StringBuilder();
// Initialize remainder
int rem = 1;
// Generate first k digits after decimal
for (int i = 0; i < k; i++) {
// Obtain next digit
int digit = (10 * rem) / n;
res.Append((char)(digit + '0'));
// Update remainder
rem = (10 * rem) % n;
}
return res.ToString();
}
public static void Main()
{
int n = 50, k = 4;
GFG obj = new GFG();
Console.WriteLine(obj.Kdigits(n, k));
}
}
function Kdigits(n, k) {
// Stores the resultant digits
let res = "";
// Initialize remainder
let rem = 1;
// Generate first k digits after decimal
for (let i = 0; i < k; i++) {
// Obtain next digit
let digit = Math.floor((10 * rem) / n);
res += digit.toString();
// Update remainder
rem = (10 * rem) % n;
}
return res;
}
// Driver Code
let n = 50, k = 4;
console.log(Kdigits(n, k));
Output
0200