Skip to content

Instantly share code, notes, and snippets.

@bennett39
Last active September 2, 2018 19:46
Show Gist options
  • Save bennett39/193a566dc38fff39aa5df6acdb807d40 to your computer and use it in GitHub Desktop.
Save bennett39/193a566dc38fff39aa5df6acdb807d40 to your computer and use it in GitHub Desktop.
Calculate Pi to Nth Digit
<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>
/*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));
});
<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