Advertisement
Guest User

Sum of squares

a guest
May 27th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 15.02 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="Author" content="Dario Alejandro Alpern" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <meta name="keywords" content="Waring, number theory, Lagrange's theorem" />
  8. <meta name="description" content="Javascript application that finds the decomposition of a number into squares. Written by Dario Alpern." />
  9. <meta name="theme-color" content="#db5945">
  10. <link rel="prefetch" href="fsquaresW0048.js" />
  11. <link rel="alternate" hreflang="es" href="SUMCUAD.HTM" />
  12. <link rel="manifest" href="fsquares.webmanifest">
  13. <link rel="icon" href="favicon.ico" type="image/x-icon" />
  14. <title>Sum of four squares</title>
  15. <style media="print">
  16. #smallheader {display:none;}
  17. </style>
  18. <style media="screen">
  19. #smallheader {background-color:#000080; width:100%; margin:0px; text-align:center;}
  20. #smallheader ul { padding:0; margin:0 auto; list-style:none; display:inline-block;}
  21. #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;}
  22. #smallheader li:hover {background-color:#004000; color:#FFFFFF;}
  23. #smallheader li ul { display:none; position:absolute; }
  24. #smallheader li:hover ul.alignleft{ display:block; height:auto;}
  25. #smallheader li:hover ul.alignright{ display:block; height:auto; right:0px; background-color:#004000;}
  26. #smallheader li ul li{ clear:both; white-space: nowrap; border:0px; background-color:#004000; width:100%; padding-top:1em; padding-bottom:0.5em}
  27. #smallheader a:link{color:#FFFFFF; text-decoration: none;}
  28. #smallheader a:visited{color:#FFFFFF; text-decoration: none;}
  29. #smallheader a:hover{background-color:#004000; color:#FFFFFF; text-decoration: none;}
  30. #smallheader a:active{background-color:#004000; color:#FFFFFF; text-decoration: none;}
  31. #smallheader li ul li a:link{background-color:#004000; color:#FFFFFF; display:block; width:100%;}
  32. #smallheader li ul li a:visited{background-color:#004000;color:#FFFFFF; display:block; width:100%;}
  33. #smallheader li ul li a:hover{background-color:#FFFFFF; color:#004000; display:block; width:100%;}
  34. #smallheader li ul li a:active{background-color:#FFFFFF; color:#004000; display:block; width:100%;}
  35. @media (max-width: 400px) { #smallheader { font-size:0.7em;} }
  36. @media (min-width: 400px) { #smallheader { font-size:1em;} }
  37. </style>
  38. <style>
  39. body {font-family: arial; margin: 0; padding: 0;}
  40. h1 {text-align:center;}
  41. .applet {margin-left: auto;margin-right: auto; border: 0px none;width:95%;text-align:center;background-color:#c0c0c0;padding:10px;}
  42. .atright {float:right;}
  43. #num {white-space:pre;overflow-wrap:normal;overflow:auto;margin:5px;width:100%;padding:3px}
  44. #stop, #cont, #wizard {display:none}
  45. .lf {padding:0.2em; clear:both; width:100%}
  46. .bigger {font-size:150%;}
  47. .pad {padding:10px;}
  48. .hex {font-family: Courier, "Lucida Console", monospace}
  49. .und {text-decoration: underline;}
  50. @media (min-width: 400px) {.input {width: calc(100% - 6em);float:right;padding:3px;margin:0px;}}
  51. @media (max-width: 400px) {.input {width:100%;padding:3px;margin:0px;}}
  52. </style>
  53. </head>
  54. <body>
  55. <nav id="smallheader">
  56. <div style="float:right;"><a href="index.htm" hreflang="es" title="Sitio de Darío Alpern en español">ESP</a></div>
  57. <ul>
  58. <li>
  59. Electronics
  60. <ul class="alignleft">
  61. <li>
  62. <a href="INTEL.HTM" hreflang="es" title="All Intel microprocessors from the 4004 up to Pentium (Spanish only)">Intel Microprocessors</a>
  63. </li>
  64. </ul>
  65. </li>
  66. <li>
  67. Mathematics
  68. <ul class="alignleft">
  69. <li>
  70. <a href="CALTORS.HTM" title="Java and Javascript programs implementing calculators">Calculators</a>
  71. </li>
  72. <li>
  73. <a href="NUMBERT.HTM" title="Articles and programs about number theory">Number Theory</a>
  74. </li>
  75. <li>
  76. <a href="PROBLEMS.HTM" title="Interesting math problems">Problems</a>
  77. </li>
  78. </ul>
  79. </li>
  80. <li>
  81. Programs
  82. <ul class="alignright">
  83. <li>
  84. <a href="ASSEM386.HTM" title="Programs written in 80386 Assembler">Assembler 80386</a>
  85. </li>
  86. <li>
  87. <a href="JAVAPROG.HTM" title="Programs written in Java">Java</a>
  88. </li>
  89. <li>
  90. <a href="GAMES.HTM" title="Computer games">Games</a>
  91. </li>
  92. </ul>
  93. </li>
  94. <li class="alignright">
  95. Contact
  96. <ul class="alignright">
  97. <li>
  98. <a href="EPERS.HTM" title="Personal information">Personal</a>
  99. </li>
  100. <li>
  101. <a href="FORM.HTM" title="Form to send comments">Comments</a>
  102. </li>
  103. <li>
  104. <a href="EGBOOK.HTM" title="Old and new guestbook">Guestbook</a>
  105. </li>
  106. <li>
  107. <a href="DONATION.HTM" title="Donations to the author of this Web site">Donations</a>
  108. </li>
  109. </ul>
  110. </li>
  111. </ul>
  112. <br style="clear:both;"/>
  113. </nav>
  114. <main id="main">
  115. <article>
  116. <h1>Sum of squares</h1>
  117. <div style='padding:10px;'>
  118. <div id="a" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemref="b" style="display:inline;">
  119. <a href="ENGLISH.HTM" itemprop="url">
  120. <span itemprop="title">Alpertron</span>
  121. </a>
  122. </div>
  123. <div id="b" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemprop="child" itemref="c" style="display:inline;">
  124. <a href="JAVAPROG.HTM" itemprop="url">
  125. <span itemprop="title">Programs</span>
  126. </a>
  127. </div>
  128. <div id="c" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemprop="child" style="display:inline;">
  129. <a href="FSQUARES.HTM" itemprop="url">
  130. <span itemprop="title">Sum of four squares</span>
  131. </a>
  132. </div>
  133. </div>
  134. <div id="applet" class="applet">
  135. <form>
  136. <label for="num">Number to be decomposed in sum of squares</label>
  137. <textarea id="num" rows="4" class="input"></textarea>
  138. <div class="lf"></div>
  139. <input type="button" id="calc" value="Sum of squares" />
  140. <input type="button" id="stop" value="Stop" />
  141. <input type="button" id="helpbtn" value="Help" />
  142. <input type="button" id="openwizard" value="Wizard" />
  143. <div class="lf"></div>
  144. <label for="digits">Number of digits per group:</label>
  145. <input type="number" id="digits" value="6"/>
  146. <input type="hidden" id="app" value="0"/>
  147. </form>
  148. </div>
  149. <script async="async" src="fsquares0048.js"></script>
  150. <div class="pad">
  151. <div id="help" aria-live="polite">
  152. <p>This applet finds the decomposition of any natural number up to 10000 digits into up to four squares.</p>
  153. <p>
  154. Notice that it does not find the prime factorization of the number entered by the user,
  155. 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>.
  156. </p>
  157. <p>
  158. Read the <a href="4SQUARES.HTM">methods used in this applet</a>.
  159. </p>
  160. <h2>Expressions</h2>
  161. <p>
  162. You can also enter expressions that use the following operators and parentheses:
  163. </p>
  164. <ul>
  165. <li> + for addition
  166. <li> - for subtraction
  167. <li> * for multiplication
  168. <li> / for integer division
  169. <li> % for modulus (remainder of the integer division)
  170. <li> ^ or ** for exponentiation (the exponent must be greater than or equal to zero).</li>
  171. <li> <strong>&lt;</strong>, <strong>==</strong>, <strong>&gt;</strong>; <strong>&lt;=</strong>, <strong>&gt;=</strong>, != for comparisons. The operators return zero for false and -1 for true.</li>
  172. <li> <strong>AND</strong>, <strong>OR</strong>, <strong>XOR</strong>, <strong>NOT</strong> for binary logic.</li>
  173. <li> <strong>SHL</strong>: Shift left the number of bits specified on the right operand.</li>
  174. <li> <strong>SHR</strong>: Shift right the number of bits specified on the right operand.</li>
  175. <li> <strong>n!</strong>: factorial (<var>n</var> must be greater than or equal to zero).</li>
  176. <li> <strong>p#</strong>: primorial (product of all primes less or equal than <var>p</var>).</li>
  177. <li> <strong>B(n)</strong>: Previous probable prime before <em>n</em></li>
  178. <li> <strong>F(n)</strong>: Fibonacci number F<sub>n</sub></li>
  179. <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>
  180. <li> <strong>N(n)</strong>: Next probable prime after <em>n</em></li>
  181. <li> <strong>P(n)</strong>: Unrestricted Partition Number (number of decompositions of <var>n</var> into sums of integers without regard to order).</li>
  182. <li> <strong>Gcd(m,n)</strong>: Greatest common divisor of these two integers.</li>
  183. <li> <strong>Modinv(m,n)</strong>: inverse of <var>m</var> modulo <var>n</var>, only valid when gcd(m,n)=1.</li>
  184. <li> <strong>Modpow(m,n,r)</strong>: finds <var>m</var><sup><var>n</var></sup> modulo <var>r</var>.</li>
  185. <li> <strong>IsPrime(n)</strong>: returns zero if <var>n</var> is not probable prime, -1 if it is.</li>
  186. <li> <strong>NumDigits(n,r)</strong>: Number of digits of <var>n</var> in base <var>r</var>.</li>
  187. <li> <strong>SumDigits(n,r)</strong>: Sum of digits of <var>n</var> in base <var>r</var>.</li>
  188. <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>
  189. </ul>
  190. <p>You can use the prefix <em>0x</em> for hexadecimal numbers, for example 0x38 is equal to 56.</p>
  191. <h2>Batch processing</h2>
  192. <p>Write an expression per line, then press the Sum if squares button. The output will be placed in the lower pane.</p>
  193. <p>Blank lines or comment lines (which start with a numeral '#' character) will be replicated on the lower pane.</p>
  194. <p>Expression loop: with the following syntax you can factor or determine primality of several numbers typing only one line.
  195. You have to type four or five expressions separated by semicolons:</p>
  196. <ul>
  197. <li>First expression: It must start with the string 'x=' and it sets the first value of x.</li>
  198. <li>Second expression: It must start with the string 'x=' and it sets the next value of x.
  199. <li>Third expression: It holds the end expression condition. If it is equal to zero (meaning false) the loop finishes, otherwise the loop continues.
  200. <li>Fourth expression: It holds the expression to be expressed as a sum of squares.</li>
  201. <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.
  202. </ul>
  203. <p>Except for the first expression, all other expressions must include the variable <var>x</var> and/or the counter <var>c</var>.</p>
  204. <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>
  205. <p>Example 1: Find the sum of squares of the numbers from 0 to 5000.
  206. The line to type is: <code>x=0;x=x+1;x&lt;=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>
  207. <p>Example 2: Find the sum of squares of the first 100 numbers of the form prime minus one.
  208. The line to type is: <code>x=3;x=n(x);c&lt;=100;x-1</code>.</p>
  209. <h2>Source code</h2>
  210. <p>
  211. 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.
  212. </p>
  213. <p>Written by Dario Alpern. Last updated 26 April 2018.</p>
  214. </div>
  215. <div id="helphelp"></div>
  216. <div id="result" aria-live="polite"></div>
  217. <div id="status"></div>
  218. <form id="cont" class="pad">
  219. <input type="button" id="continue" value="Continue" />
  220. </form>
  221. <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>
  222. </div>
  223. </article>
  224. </main>
  225. <aside id="wizard">
  226. <h1>Sum of squares wizard</h1>
  227. <form class="applet">
  228. <fieldset id="output" class="atright">
  229. <legend>Output</legend>
  230. <input type="radio" name="output" id="decW"><label for="decW"><span class="und">D</span>ecimal</label><br>
  231. <input type="radio" name="output" id="hexW"><label for="hexW"><span class="und">H</span>exadecimal</label><br>
  232. </fieldset>
  233. <fieldset id="mode">
  234. <legend>Wizard mode</legend>
  235. <input type="radio" name="mode" id="oneexpr"><label for="oneexpr"><span class="und">P</span>rocess one expression</label><br>
  236. <input type="radio" name="mode" id="loop"><label for="loop"><span class="und">P</span>rocess several expressions in a loop</label><br>
  237. </fieldset>
  238. <label for="wzdinput" id="wzddesc">&nbsp;</label>
  239. <br class="newline"/>
  240. <input type="text" id="wzdinput" value="" placeholder="Number or numerical expression" class="input"/>
  241. <br class="newline"/>
  242. <p id="wzdexam">&nbsp;</p>
  243. <input type="button" id="next" value="Next" />
  244. <input type="button" id="cancel" value="Cancel" />
  245. </form>
  246. <h2>Expressions</h2>
  247. <p>You can enter expressions that use the following operators, functions and parentheses:</p>
  248. <p>
  249. <ul>
  250. <li> + for addition
  251. <li> - for subtraction
  252. <li> * for multiplication
  253. <li> / for integer division
  254. <li> % for modulus (remainder of the integer division)
  255. <li> ^ or ** for exponentiation (the exponent must be greater than or equal to zero).</li>
  256. <li> <strong>&lt;</strong>, <strong>==</strong>, <strong>&gt;</strong>; <strong>&lt;=</strong>, <strong>&gt;=</strong>, != for comparisons. The operators return zero for false and -1 for true.</li>
  257. <li> <strong>AND</strong>, <strong>OR</strong>, <strong>XOR</strong>, <strong>NOT</strong> for binary logic.
  258. <li> <strong>SHL</strong>: Shift left the number of bits specified on the right operand.
  259. <li> <strong>SHR</strong>: Shift right the number of bits specified on the right operand.
  260. <li> <strong>n!</strong>: factorial (<var>n</var> must be greater than or equal to zero).</li>
  261. <li> <strong>p#</strong>: primorial (product of all primes less or equal than <var>p</var>).</li>
  262. <li> <strong>B(n)</strong>: Previous probable prime before <em>n</em></li>
  263. <li> <strong>F(n)</strong>: Fibonacci number F<sub>n</sub></li>
  264. <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>
  265. <li> <strong>N(n)</strong>: Next probable prime after <em>n</em></li>
  266. <li> <strong>P(n)</strong>: Unrestricted Partition Number (number of decompositions of <var>n</var> into sums of integers without regard to order).</li>
  267. <li> <strong>Gcd(m,n)</strong>: Greatest common divisor of these two integers.</li>
  268. <li> <strong>Modinv(m,n)</strong>: inverse of <var>m</var> modulo <var>n</var>, only valid when gcd(m,n)=1.</li>
  269. <li> <strong>Modpow(m,n,r)</strong>: finds <var>m</var><sup><var>n</var></sup> modulo <var>r</var>.</li>
  270. <li> <strong>IsPrime(n)</strong>: returns zero if <var>n</var> is not probable prime, -1 if it is.</li>
  271. <li> <strong>NumDigits(n,r)</strong>: Number of digits of <var>n</var> in base <var>r</var>.</li>
  272. <li> <strong>SumDigits(n,r)</strong>: Sum of digits of <var>n</var> in base <var>r</var>.</li>
  273. <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>
  274. <li id="C">Variable <var>C</var>: number of expressions already processed.</li>
  275. <li id="X">Variable <var>X</var>: variable changed on each iteration of the loop.</li>
  276. </ul>
  277. <p>You can use the prefix <em>0x</em> for hexadecimal numbers, for example 0x38 is equal to 56.</p>
  278. </aside>
  279. </body>
  280. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement