in Education by
I am learning bash scripting. While exploring the math functions i am came across a command which calculated the value of pi. seq -f '4/%g' 1 2 99999 | paste -sd-+ | bc -l Although i understand how the basic seq command works, I am unable to understand how does the above command works. Can anybody please clarify how does it work.? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This calculates the value of π using Gregory–Leibniz series: seq -f '4/%g' 1 2 99999 generates the fractions: 4/1 4/3 4/5 4/7 4/9 4/11 4/13 4/15 4/17 4/19 The paste pipeline paste -sd-+ combines those with alternate delimiters - and +. Finally, bc -l performs the arithmetic to give the result. EDIT: As noted in the comment, this sequence converges very slowly. Machin's formula has a significantly higher rate of convergence: Using the same expansion for tan-1(x): to compute π, we can see that it produces the correct value to 50 digits1 using just the first 50 terms of the series: $ { echo -n "scale=50;"; seq 1 2 100 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l 3.14159265358979323846264338327950288419716939937510 With just 100 terms, the value of π is computed accurately to more than 100 digits: $ { echo -n "scale=100;"; seq 1 2 200 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 1 Pi

Related questions

0 votes
    It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    !conda --version /bin/bash: conda: command not found I have installed miniconda and have chosen to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Hi I want to remove last comma from a line. For example: Input: This,is,a,test Desired Output: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    Given a string file path such as /foo/fizzbuzz.bar, how would I use bash to extract just the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    How can you calculate accuracy using a confusion matrix?...
asked Apr 27, 2021 in Technology by JackTerrance
0 votes
    Yesterday I installed bash on Windows 10 and also installed Git on it. It works perfectly fine. Following ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm starting a bash script which will take a path in S3 (as specified to the ls command) and dump the ... finding which does it? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Using which component you can calculate the processing time of one or more Subjobs in the main Job? tFlowMeter tChronometerStart tFlowMeterCatcher tStatCatcher...
asked Mar 25, 2021 in Technology by JackTerrance
0 votes
    Can Raspberry Pi be used as a server?...
asked Jan 20, 2023 in Technology by JackTerrance
0 votes
    A can hit a target 4 times in 5 shots, b can hit 3 times in 4 shots, and C can hit 2 times in 3 shots ... A does not hit the target. Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
0 votes
    the directory /Library/Frameworks/Python.framework/ contains the following four elements: Headers Python Resources ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Quick-and-dirty way to ensure only one instance of a shell script ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Why doesnt "tail" work to truncate log files? (6 answers) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Why doesnt "tail" work to truncate log files? (6 answers) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...