Last active
September 2, 2018 19:46
-
-
Save bennett39/193a566dc38fff39aa5df6acdb807d40 to your computer and use it in GitHub Desktop.
Calculate Pi to Nth Digit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label for="digits">How many digits of pi do you need? (30 max)</label> | |
<br /> | |
<input type="number" id="digits"> | |
<br /> | |
<button type="submit" id="submit">Submit</button> | |
<p id="result"></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Return pi to the nth digit using Javascript and JQ | |
function calculatePi(n) { | |
//Input too long or not a number, default to 30 | |
if (n === undefined || n > 30) { | |
n = 30; | |
} | |
//Machin's formula for pi: | |
return (16 * Math.atan(1 / 5) - 4 * Math.atan(1 / 239)).toFixed(n); | |
} | |
$("#submit").click(function() { | |
var digits = $("#digits").val(); | |
$("#result").html(calculatePi(digits)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment