Skip to content

Instantly share code, notes, and snippets.

@p13i
Last active August 29, 2015 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p13i/a258727131e6c639ec54 to your computer and use it in GitHub Desktop.
Save p13i/a258727131e6c639ec54 to your computer and use it in GitHub Desktop.
// FOR THE PURPOSES of this code sample, some code essential
// for the functioning of this script was removed.
// Please view the "Finding PPTs" page source of
// http://pramodk.net/GT/MATH2803/PPTs/
// to see a functional example.
$("input[name='submit']").click(function () { // Upon clicking the submit button, run this function
// Get the input values
var maxS = $("input[name='s_bound']").val();
var maxT = $("input[name='s_bound']").val();
// Return if there is invalid data
if ((maxS === 0 || maxS === "") || (maxT === 0 || maxT === "")) {
alert("One (or both) of your value(s) is/are invalid or null. Please enter a value or larger number and submit again.");
return;
}
// Empty the current table contents
$("#PPTs tbody").empty();
// START: helper functions
// Helper: check if number is integer
var isInt = function(n) {
if (n % 1 === 0) {
return true;
}
return false;
};
// Helper: find GDC of two numbers (i.e. check if they are coprime)
var gcd = function(a, b) {
if (!b) {
return a;
}
return gcd(b, a % b);
};
// Main helper: get PPT from s and t values; returns PPT only if it is valid
var getPPT = function(s, t) {
var a = Math.abs((Math.pow(t, 2) - Math.pow(s, 2)));
var b = 2 * s * t;
var c = (Math.pow(s, 2) + Math.pow(t, 2));
// return false if any value is 0, just for simplicity
if ((a === 0) || (b === 0) || (c === 0)) {
return false;
}
// Check if a and c are integers, check coprime of one pair, check Pythagorean Theorem
else if (isInt(a) && isInt(c) && (gcd(a, b) === 1) && (Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2))) {
return new Array(a, b, c);
}
return false;
};
// END: helper functions
// Declare new array of PPTs
var PPTs = new Array();
// For all s and t values less than or equal to the maxS and maxT values, respectively, try to get a PPT for s and t
for (var s = 0; s <= maxS; s++) {
for (var t = 0; t <= maxT; t++) {
var PPT = getPPT(s, t);
if (PPT && (JSON.stringify(PPTs).indexOf(JSON.stringify(PPT)) === -1)) { // This searching method could be better :)
PPTs.push(PPT); // Our continously expanding array of valid PPTs
}
}
}
// Append rows to our table
for (var i = 0; i < PPTs.length; i++) {
$("#PPTs tbody").append("<tr><td>" + PPTs[i][0] + "</td><td>" + PPTs[i][1] + "</td><td>" + PPTs[i][2] + "</td></tr>");
}
}
// JavaScript and logic by Pramod Kotipalli (pramodk.net)
// Created for Matthew Baker's course
// (Math 2803: Number Theory and Cryptography)
// at the Georgia Institute of Technology
// --------------------------------------------------------
// Use freely under these conditions:
// Attribution-NonCommercial 4.0 International
// (http://creativecommons.org/licenses/by-nc/4.0/)
// --------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment