Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <meta name="Author" content="Dario Alejandro Alpern" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <meta name="keywords" content="Waring, number theory, Lagrange's theorem" />
- <meta name="description" content="Javascript application that finds the decomposition of a number into squares. Written by Dario Alpern." />
- <meta name="theme-color" content="#db5945">
- <link rel="prefetch" href="fsquaresW0048.js" />
- <link rel="alternate" hreflang="es" href="SUMCUAD.HTM" />
- <link rel="manifest" href="fsquares.webmanifest">
- <link rel="icon" href="favicon.ico" type="image/x-icon" />
- <title>Sum of four squares</title>
- <style media="print">
- #smallheader {display:none;}
- </style>
- <style media="screen">
- #smallheader {background-color:#000080; width:100%; margin:0px; text-align:center;}
- #smallheader ul { padding:0; margin:0 auto; list-style:none; display:inline-block;}
- #smallheader li { float:left; position:relative; display:block; margin-top:0px; margin-bottom:0px; margin-left:5px; margin-right:5px; background-color:#000080; color:#FFFFFF; font-family:"Arial", sans-serif; cursor: pointer; text-align:left;}
- #smallheader li:hover {background-color:#004000; color:#FFFFFF;}
- #smallheader li ul { display:none; position:absolute; }
- #smallheader li:hover ul.alignleft{ display:block; height:auto;}
- #smallheader li:hover ul.alignright{ display:block; height:auto; right:0px; background-color:#004000;}
- #smallheader li ul li{ clear:both; white-space: nowrap; border:0px; background-color:#004000; width:100%; padding-top:1em; padding-bottom:0.5em}
- #smallheader a:link{color:#FFFFFF; text-decoration: none;}
- #smallheader a:visited{color:#FFFFFF; text-decoration: none;}
- #smallheader a:hover{background-color:#004000; color:#FFFFFF; text-decoration: none;}
- #smallheader a:active{background-color:#004000; color:#FFFFFF; text-decoration: none;}
- #smallheader li ul li a:link{background-color:#004000; color:#FFFFFF; display:block; width:100%;}
- #smallheader li ul li a:visited{background-color:#004000;color:#FFFFFF; display:block; width:100%;}
- #smallheader li ul li a:hover{background-color:#FFFFFF; color:#004000; display:block; width:100%;}
- #smallheader li ul li a:active{background-color:#FFFFFF; color:#004000; display:block; width:100%;}
- @media (max-width: 400px) { #smallheader { font-size:0.7em;} }
- @media (min-width: 400px) { #smallheader { font-size:1em;} }
- </style>
- <style>
- body {font-family: arial; margin: 0; padding: 0;}
- h1 {text-align:center;}
- .applet {margin-left: auto;margin-right: auto; border: 0px none;width:95%;text-align:center;background-color:#c0c0c0;padding:10px;}
- .atright {float:right;}
- #num {white-space:pre;overflow-wrap:normal;overflow:auto;margin:5px;width:100%;padding:3px}
- #stop, #cont, #wizard {display:none}
- .lf {padding:0.2em; clear:both; width:100%}
- .bigger {font-size:150%;}
- .pad {padding:10px;}
- .hex {font-family: Courier, "Lucida Console", monospace}
- .und {text-decoration: underline;}
- @media (min-width: 400px) {.input {width: calc(100% - 6em);float:right;padding:3px;margin:0px;}}
- @media (max-width: 400px) {.input {width:100%;padding:3px;margin:0px;}}
- </style>
- </head>
- <body>
- <nav id="smallheader">
- <div style="float:right;"><a href="index.htm" hreflang="es" title="Sitio de Darío Alpern en español">ESP</a></div>
- <ul>
- <li>
- Electronics
- <ul class="alignleft">
- <li>
- <a href="INTEL.HTM" hreflang="es" title="All Intel microprocessors from the 4004 up to Pentium (Spanish only)">Intel Microprocessors</a>
- </li>
- </ul>
- </li>
- <li>
- Mathematics
- <ul class="alignleft">
- <li>
- <a href="CALTORS.HTM" title="Java and Javascript programs implementing calculators">Calculators</a>
- </li>
- <li>
- <a href="NUMBERT.HTM" title="Articles and programs about number theory">Number Theory</a>
- </li>
- <li>
- <a href="PROBLEMS.HTM" title="Interesting math problems">Problems</a>
- </li>
- </ul>
- </li>
- <li>
- Programs
- <ul class="alignright">
- <li>
- <a href="ASSEM386.HTM" title="Programs written in 80386 Assembler">Assembler 80386</a>
- </li>
- <li>
- <a href="JAVAPROG.HTM" title="Programs written in Java">Java</a>
- </li>
- <li>
- <a href="GAMES.HTM" title="Computer games">Games</a>
- </li>
- </ul>
- </li>
- <li class="alignright">
- Contact
- <ul class="alignright">
- <li>
- <a href="EPERS.HTM" title="Personal information">Personal</a>
- </li>
- <li>
- <a href="FORM.HTM" title="Form to send comments">Comments</a>
- </li>
- <li>
- <a href="EGBOOK.HTM" title="Old and new guestbook">Guestbook</a>
- </li>
- <li>
- <a href="DONATION.HTM" title="Donations to the author of this Web site">Donations</a>
- </li>
- </ul>
- </li>
- </ul>
- <br style="clear:both;"/>
- </nav>
- <main id="main">
- <article>
- <h1>Sum of squares</h1>
- <div style='padding:10px;'>
- <div id="a" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemref="b" style="display:inline;">
- <a href="ENGLISH.HTM" itemprop="url">
- <span itemprop="title">Alpertron</span>
- </a> ›
- </div>
- <div id="b" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemprop="child" itemref="c" style="display:inline;">
- <a href="JAVAPROG.HTM" itemprop="url">
- <span itemprop="title">Programs</span>
- </a> ›
- </div>
- <div id="c" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemprop="child" style="display:inline;">
- <a href="FSQUARES.HTM" itemprop="url">
- <span itemprop="title">Sum of four squares</span>
- </a>
- </div>
- </div>
- <div id="applet" class="applet">
- <form>
- <label for="num">Number to be decomposed in sum of squares</label>
- <textarea id="num" rows="4" class="input"></textarea>
- <div class="lf"></div>
- <input type="button" id="calc" value="Sum of squares" />
- <input type="button" id="stop" value="Stop" />
- <input type="button" id="helpbtn" value="Help" />
- <input type="button" id="openwizard" value="Wizard" />
- <div class="lf"></div>
- <label for="digits">Number of digits per group:</label>
- <input type="number" id="digits" value="6"/>
- <input type="hidden" id="app" value="0"/>
- </form>
- </div>
- <script async="async" src="fsquares0048.js"></script>
- <div class="pad">
- <div id="help" aria-live="polite">
- <p>This applet finds the decomposition of any natural number up to 10000 digits into up to four squares.</p>
- <p>
- Notice that it does not find the prime factorization of the number entered by the user,
- so some cases where two squares would be enough, it will be represented as a sum of three squares. For example, 10000998089 = 95317<span class="bigger">²</span> + 30260<span class="bigger">²</span>.
- </p>
- <p>
- Read the <a href="4SQUARES.HTM">methods used in this applet</a>.
- </p>
- <h2>Expressions</h2>
- <p>
- You can also enter expressions that use the following operators and parentheses:
- </p>
- <ul>
- <li> + for addition
- <li> - for subtraction
- <li> * for multiplication
- <li> / for integer division
- <li> % for modulus (remainder of the integer division)
- <li> ^ or ** for exponentiation (the exponent must be greater than or equal to zero).</li>
- <li> <strong><</strong>, <strong>==</strong>, <strong>></strong>; <strong><=</strong>, <strong>>=</strong>, != for comparisons. The operators return zero for false and -1 for true.</li>
- <li> <strong>AND</strong>, <strong>OR</strong>, <strong>XOR</strong>, <strong>NOT</strong> for binary logic.</li>
- <li> <strong>SHL</strong>: Shift left the number of bits specified on the right operand.</li>
- <li> <strong>SHR</strong>: Shift right the number of bits specified on the right operand.</li>
- <li> <strong>n!</strong>: factorial (<var>n</var> must be greater than or equal to zero).</li>
- <li> <strong>p#</strong>: primorial (product of all primes less or equal than <var>p</var>).</li>
- <li> <strong>B(n)</strong>: Previous probable prime before <em>n</em></li>
- <li> <strong>F(n)</strong>: Fibonacci number F<sub>n</sub></li>
- <li> <strong>L(n)</strong>: Lucas number L<sub>n</sub> = F<sub><var>n</var>-1</sub> + F<sub><var>n</var>+1</sub></li>
- <li> <strong>N(n)</strong>: Next probable prime after <em>n</em></li>
- <li> <strong>P(n)</strong>: Unrestricted Partition Number (number of decompositions of <var>n</var> into sums of integers without regard to order).</li>
- <li> <strong>Gcd(m,n)</strong>: Greatest common divisor of these two integers.</li>
- <li> <strong>Modinv(m,n)</strong>: inverse of <var>m</var> modulo <var>n</var>, only valid when gcd(m,n)=1.</li>
- <li> <strong>Modpow(m,n,r)</strong>: finds <var>m</var><sup><var>n</var></sup> modulo <var>r</var>.</li>
- <li> <strong>IsPrime(n)</strong>: returns zero if <var>n</var> is not probable prime, -1 if it is.</li>
- <li> <strong>NumDigits(n,r)</strong>: Number of digits of <var>n</var> in base <var>r</var>.</li>
- <li> <strong>SumDigits(n,r)</strong>: Sum of digits of <var>n</var> in base <var>r</var>.</li>
- <li> <strong>RevDigits(n,r)</strong>: finds the value obtained by writing backwards the digits of <var>n</var> in base <var>r</var>.</li>
- </ul>
- <p>You can use the prefix <em>0x</em> for hexadecimal numbers, for example 0x38 is equal to 56.</p>
- <h2>Batch processing</h2>
- <p>Write an expression per line, then press the Sum if squares button. The output will be placed in the lower pane.</p>
- <p>Blank lines or comment lines (which start with a numeral '#' character) will be replicated on the lower pane.</p>
- <p>Expression loop: with the following syntax you can factor or determine primality of several numbers typing only one line.
- You have to type four or five expressions separated by semicolons:</p>
- <ul>
- <li>First expression: It must start with the string 'x=' and it sets the first value of x.</li>
- <li>Second expression: It must start with the string 'x=' and it sets the next value of x.
- <li>Third expression: It holds the end expression condition. If it is equal to zero (meaning false) the loop finishes, otherwise the loop continues.
- <li>Fourth expression: It holds the expression to be expressed as a sum of squares.</li>
- <li>Optional fifth expression: If this expression is different from zero (meaning true), the fourth expression is processed, and if is zero (meaning false), the fourth expression is ignored.
- </ul>
- <p>Except for the first expression, all other expressions must include the variable <var>x</var> and/or the counter <var>c</var>.</p>
- <p>If the end expression is false after processing 1000 numbers, the Continue button will appear. Pressing this button will make the program to process the next 1000 numbers and so on.</p>
- <p>Example 1: Find the sum of squares of the numbers from 0 to 5000.
- The line to type is: <code>x=0;x=x+1;x<=5000;x</code>. The calculator will show the results in blocks of 1000 values. You will need to press the Continue button to get the next block.</p>
- <p>Example 2: Find the sum of squares of the first 100 numbers of the form prime minus one.
- The line to type is: <code>x=3;x=n(x);c<=100;x-1</code>.</p>
- <h2>Source code</h2>
- <p>
- You can download the source of the current program and the old sum of four squares applet from <a href="https://github.com/alpertron/calculators">GitHub</a>. Notice that the source code is in C language and you need the <a href="https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html">Emscripten</a> environment in order to generate Javascript.
- </p>
- <p>Written by Dario Alpern. Last updated 26 April 2018.</p>
- </div>
- <div id="helphelp"></div>
- <div id="result" aria-live="polite"></div>
- <div id="status"></div>
- <form id="cont" class="pad">
- <input type="button" id="continue" value="Continue" />
- </form>
- <p>If you find any error or you have a comment, please fill in the <a HREF="FORM.HTM?Sum+of+squares+feedback">form</a>.</p>
- </div>
- </article>
- </main>
- <aside id="wizard">
- <h1>Sum of squares wizard</h1>
- <form class="applet">
- <fieldset id="output" class="atright">
- <legend>Output</legend>
- <input type="radio" name="output" id="decW"><label for="decW"><span class="und">D</span>ecimal</label><br>
- <input type="radio" name="output" id="hexW"><label for="hexW"><span class="und">H</span>exadecimal</label><br>
- </fieldset>
- <fieldset id="mode">
- <legend>Wizard mode</legend>
- <input type="radio" name="mode" id="oneexpr"><label for="oneexpr"><span class="und">P</span>rocess one expression</label><br>
- <input type="radio" name="mode" id="loop"><label for="loop"><span class="und">P</span>rocess several expressions in a loop</label><br>
- </fieldset>
- <label for="wzdinput" id="wzddesc"> </label>
- <br class="newline"/>
- <input type="text" id="wzdinput" value="" placeholder="Number or numerical expression" class="input"/>
- <br class="newline"/>
- <p id="wzdexam"> </p>
- <input type="button" id="next" value="Next" />
- <input type="button" id="cancel" value="Cancel" />
- </form>
- <h2>Expressions</h2>
- <p>You can enter expressions that use the following operators, functions and parentheses:</p>
- <p>
- <ul>
- <li> + for addition
- <li> - for subtraction
- <li> * for multiplication
- <li> / for integer division
- <li> % for modulus (remainder of the integer division)
- <li> ^ or ** for exponentiation (the exponent must be greater than or equal to zero).</li>
- <li> <strong><</strong>, <strong>==</strong>, <strong>></strong>; <strong><=</strong>, <strong>>=</strong>, != for comparisons. The operators return zero for false and -1 for true.</li>
- <li> <strong>AND</strong>, <strong>OR</strong>, <strong>XOR</strong>, <strong>NOT</strong> for binary logic.
- <li> <strong>SHL</strong>: Shift left the number of bits specified on the right operand.
- <li> <strong>SHR</strong>: Shift right the number of bits specified on the right operand.
- <li> <strong>n!</strong>: factorial (<var>n</var> must be greater than or equal to zero).</li>
- <li> <strong>p#</strong>: primorial (product of all primes less or equal than <var>p</var>).</li>
- <li> <strong>B(n)</strong>: Previous probable prime before <em>n</em></li>
- <li> <strong>F(n)</strong>: Fibonacci number F<sub>n</sub></li>
- <li> <strong>L(n)</strong>: Lucas number L<sub>n</sub> = F<sub><var>n</var>-1</sub> + F<sub><var>n</var>+1</sub></li>
- <li> <strong>N(n)</strong>: Next probable prime after <em>n</em></li>
- <li> <strong>P(n)</strong>: Unrestricted Partition Number (number of decompositions of <var>n</var> into sums of integers without regard to order).</li>
- <li> <strong>Gcd(m,n)</strong>: Greatest common divisor of these two integers.</li>
- <li> <strong>Modinv(m,n)</strong>: inverse of <var>m</var> modulo <var>n</var>, only valid when gcd(m,n)=1.</li>
- <li> <strong>Modpow(m,n,r)</strong>: finds <var>m</var><sup><var>n</var></sup> modulo <var>r</var>.</li>
- <li> <strong>IsPrime(n)</strong>: returns zero if <var>n</var> is not probable prime, -1 if it is.</li>
- <li> <strong>NumDigits(n,r)</strong>: Number of digits of <var>n</var> in base <var>r</var>.</li>
- <li> <strong>SumDigits(n,r)</strong>: Sum of digits of <var>n</var> in base <var>r</var>.</li>
- <li> <strong>RevDigits(n,r)</strong>: finds the value obtained by writing backwards the digits of <var>n</var> in base <var>r</var>.</li>
- <li id="C">Variable <var>C</var>: number of expressions already processed.</li>
- <li id="X">Variable <var>X</var>: variable changed on each iteration of the loop.</li>
- </ul>
- <p>You can use the prefix <em>0x</em> for hexadecimal numbers, for example 0x38 is equal to 56.</p>
- </aside>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement