Approximately Codechef Solution|Problem Code: APPROX

Hello coders, today we are going to solve Approximately Codechef Solution.

Approximately Codechef Solution
Approximately Codechef Solution

Problem

Chef has recently learnt some new facts about the famous number π. For example, he was surprised that ordinary fractions are sometimes used to represent this number approximately. For example, 22/7355/113 or even 103993/33102.

Soon, by calculating the value of 22/7 and 355/113 on paper Chef became quite disappointed because these values are not precise enough. For example, 22/7 differs in the third digit after the decimal point. So, these values are definitely should not be used for serious calculations.

However, Chef doesn’t know anything about 103993/33102. This fraction is quite inconvenient to calculate on paper. Chef is curious how precise this value is. So he asks you to help him and to calculate the first K digits after the decimal point of such an approximation of π. He consider this ordinary fraction as infinite decimal fraction so formally he asks you to calculate this approximation truncated to the first K digits after the decimal point.

Input

The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer K.

Output

For each test case output a single line containing the value of 103993/33102 truncated to the first K digits after the decimal point. Note that for K = 0 you should output just “3” without decimal point (quotes are for clarity).

Constraints

  • 0 ≤ K ≤ 106
  • 1 ≤ T ≤ 2000
  • The sum of K over the input does not exceed 106

Example

Sample Input 1 

3
6
20

Sample Output 1 

3
3.141592
3.14159265301190260407

Approximately CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int K = sc.nextInt();
			System.out.println(solve(K));
		}
		sc.close();
	}
	static String solve(int K) {
		StringBuilder result = new StringBuilder();
		int numerator = 103993;
		int denominator = 33102;
		for (int i = 0; i < K + 1; i++) {
			if (i == 1) {
				result.append(".");
			}
			result.append(numerator / denominator);
			numerator = numerator % denominator * 10;
		}
		return result.toString();
	}
}

Approximately CodeChef Solution in CPP

#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--){
        int a=103993;
        int b=33102;
        int c=a%b;
        int k;
        cin>>k;
        c=c*10;
        cout<<3;
        if(k>0){
            cout<<".";
            while(k--){
                cout<<c/b;
                c=(c%b)*10;
            }
        }
        cout<<endl;
    }
	return 0;
}

Approximately CodeChef Solution in Python

tc = int(input())
for _ in range(tc):
    k = int(input())
    ans = '3'
    if(k == 0):
        print(ans)
        continue
    ans = ans + '.'
    num = 4687
    den = 33102
    for i in range(k):
        num *= 10
        ans += str(num//den)
        num = num % den
    print(ans)

Disclaimer: The above Problem (Approximately) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring

Leave a Comment