First Occurrence of a Digit in a Given Fraction


The decimal expansion of a fraction is the decimal representation of the fraction's value. In the following article we discuss two approaches to find the first occurrence of c in a/b.

Problem Statement

Given three integers a, b, c, locate the first instance of c in the fraction a/b after the decimal point. Print-1 if it does not exist.

Sample Examples

Input

a = 5, b = 6, c = 3

Output

2

Explanation

$$\mathrm{\frac{a}{b}=\frac{5}{6}=0.83333}$$

So c=3 occurs at the 2nd place after the decimal point. Hence the output is 2.

Input

a = -10, b = 25, c = 4

Output

1

Explanation

$$\mathrm{\frac{a}{b}=\frac{-10}{25}=-0.4}$$

So c=4 occurs at the 1st place after the decimal point. Hence the output is 1.

Input

a = 47, b = 9, c = 3

Output

-1

Explanation

$$\mathrm{\frac{a}{b}=\frac{47}{9}=5.222}$$

So c=3 does not occur at any point after the decimal. It does not exist; hence the output is -1.

Naive Solution Approach

The basic idea is to store the quotient obtained after dividing a by b. We convert the result obtained into a string and check for the first occurrence of c after the decimal point. The approach is intuitive and easy to understand; however, it gives an incorrect result when the programming language rounds-off the answer.

For example,

Let a = 4, b = 6, c = 7

According to this approach, a/b = 4/6 = 0.66667. Hence it will show the first occurrence of c = 7 at the 5th position whereas it should be -1.

An implementation of the approach can be understood by the C++ program provided below.

Example: C++ Program

The following program code basically finds the first occurrence of a number in the decimal expansion after the decimal point. The function find_first_occurrence() stores the decimal expansion of a/b in variable q. We convert q to a string and iterate over the string to find the first occurrence of c after the decimal point and return it as pos. If pos = -1, it means the digit does not exist after the decimal point.

// C++ program to find the first occurrence of a number in a fraction
#include <bits/stdc++.h>
using namespace std;
// function to find the first occurrence of c after the decimal point in decimal expansion of a/b
int find_first_occurrence(int a, int b, int c){
    // q = quotient = decimal expansion of a/b
    float q = (float)a / b;
    // convert q to string using to_string() function
    string s = to_string(q);
    // increment i till we reach decimal point
    int i = 0;
    while (i < s.length()){
        if (s[i] != '.')
        {
            i++;
        }
        else
        {
            break;
        }
    }
    // if decimal point does not exist i.e a is completely divisible by b; return -1 i.e. the position does not exist
    if (i >= s.length()){
        return -1;
    }
    // increment i to point to the first value after the decimal point
    i = i + 1;
    // traverse the string to find the first occurrence of c after the decimal point
    for (i; i < s.length(); i++){
        if (s[i] == (c + '0')){
            break;
        }
    }
    return i - 1;
}
// main function
int main(){
    int a = 22;
    int b = 7;
    int c = 2;
    int pos = find_first_occurrence(a, b, c);
    if (pos != -1){
        cout << "The digit " << c << " first occurs at position " << pos << " after the decimal point.";
    }
    else{
        cout << "The digit " << c << " is not found in the decimal expansion of the fraction " << a << "/" << b;
    }
    return 0;
}

Output

The digit 2 first occurs at position 3 after the decimal point.

Time and Space Complexity

O(d)where d is the number of digits after the decimal point in the decimal expansion of a/b.

Efficient Solution Approach

The naive approach had the drawback that the program rounds off the quotient which introduces integer values which are not supposed to be there in the decimal expansion of a/b as it may yield incorrect results in some cases as explained above.

An efficient approach is to first reduce the fraction to its mod (a %= b) and then traverse each decimal place until either the digit ‘c’ is found or a maximum number of iterations is reached to avoid an infinite loop.

A while loop runs until ‘a’ becomes zero or the maximum number of iterations is reached. In each iteration, the decimal place is obtained by multiplying the remainder (a % b) by 10 and then getting the quotient of the result and ‘b’ (q = a / b). If the obtained quotient is equal to the required digit c, then the function returns the current iteration count.

If the required digit c is not found after the maximum iterations, then the function returns -1 to indicate that the digit was not found.

Algorithm

Function find_first_occurrence(a, b, c)

  • While a is greater than or equal to b

    • Set a to a - b

  • Set i to 1

  • Set q to a divided by b

  • Set max_iterations to 1000

  • While a is not equal to 0 and i is less than or equal to max_iterations do the following:

    • Set a to (a modulo b) multiplied by 10

    • Set q to a divided by b

    • If q is equal to c, return i

    • Increment i

  • Return -1 to indicate that c was not found after the decimal point.

Main function

  • Set a to 22, b to 7, and c to 2

  • Print the value of a divided by b

  • Call the function find_first_occurrence with arguments a, b, and c

  • Print the return value of find_first_occurrence

Example: C++ Program

This program aims to find the first occurrence of a specific digit ‘c’ after the decimal point in the decimal expansion of a fraction a/b. It reduces the fraction to its mod and then traverses each decimal place until either the digit c is found or a maximum number of iterations is reached to avoid an infinite loop. If the digit is found, the current iteration count is returned, otherwise, the function returns -1 to indicate that the digit was not found.

// C++ program to find the first occurrence of a number in a fraction
#include <iostream>
using namespace std;
// function to find the first occurrence of c after the decimal point in decimal expansion of a/b
int find_first_occurrence(int a, int b, int c){
    // Reduce the number to its mod
    while (a >= b){
        a -= b;
    }
    // Traverse for every decimal place
    int i = 1;
    int q = a / b;
    int max_iterations = 1000; // set a maximum number of iterations to avoid an infinite loop
    while (a != 0 && i <= max_iterations) {
        a = (a % b) * 10;
        q = a / b;
        if (q == c) {
            return i;
        }
        i++;
    }
    // If digit not found
    return -1;
}
//main function
int main(){
    int a = 22, b = 7, c = 2;
    cout << find_first_occurrence(a, b, c);
    return 0;
}

Output

3

Time and Space Complexity Analysis

Time Complexity: O(max_iterations)

The time complexity of the given code depends on the value of the maximum number of iterations max_iterations. The while loop for traversing the decimal places of the fraction executes up to max_iterations times. Hence, the time complexity is O(max_iterations).

Space Complexity: O(1)

The space complexity of the code is O(1) as the amount of memory used does not depend on the size of the input. The only variables used in the code are a, b, c, i, q, and max_iterations. All of these variables take constant space.

Conclusion

This article discusses two approaches to find the first occurrence of a given number after the decimal point in the decimal expansion of a fraction. The concept of the approaches, examples, algorithm used, C++ program solution as well as the time and space complexity analysis were explained thoroughly for a deeper understanding.

Updated on: 08-Sep-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements