Sum of squares
This applet finds the decomposition of any natural number up to 10000 digits into up to four squares.
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² + 30260².
Read the methods used in this applet.
You can also enter expressions that use the following operators and parentheses:
- + for addition
- - for subtraction
- * for multiplication
- / for integer division
- % for modulus (remainder of the integer division)
- ^ or ** for exponentiation (the exponent must be greater than or equal to zero).
- <, ==, >; <=, >=, != for comparisons. The operators return zero for false and -1 for true.
- AND, OR, XOR, NOT for binary logic.
- SHL: Shift left the number of bits specified on the right operand.
- SHR: Shift right the number of bits specified on the right operand.
- n!: factorial (n must be greater than or equal to zero).
- p#: primorial (product of all primes less or equal than p).
- B(n): Previous probable prime before n
- F(n): Fibonacci number Fn
- L(n): Lucas number Ln = Fn-1 + Fn+1
- N(n): Next probable prime after n
- P(n): Unrestricted Partition Number (number of decompositions of n into sums of integers without regard to order).
- Gcd(m,n): Greatest common divisor of these two integers.
- Modinv(m,n): inverse of m modulo n, only valid when gcd(m,n)=1.
- Modpow(m,n,r): finds mn modulo r.
- IsPrime(n): returns zero if n is not probable prime, -1 if it is.
- NumDigits(n,r): Number of digits of n in base r.
- SumDigits(n,r): Sum of digits of n in base r.
- RevDigits(n,r): finds the value obtained by writing backwards the digits of n in base r.
You can use the prefix 0x for hexadecimal numbers, for example 0x38 is equal to 56.
Write an expression per line, then press the Sum if squares button. The output will be placed in the lower pane.
Blank lines or comment lines (which start with a numeral '#' character) will be replicated on the lower pane.
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:
- First expression: It must start with the string 'x=' and it sets the first value of x.
- Second expression: It must start with the string 'x=' and it sets the next value of x.
- Third expression: It holds the end expression condition. If it is equal to zero (meaning false) the loop finishes, otherwise the loop continues.
- Fourth expression: It holds the expression to be expressed as a sum of squares.
- 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.
Except for the first expression, all other expressions must include the variable x and/or the counter c.
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.
Example 1: Find the sum of squares of the numbers from 0 to 5000.
The line to type is:
x=0;x=x+1;x<=5000;x. The calculator will show the results in blocks of 1000 values. You will need to press the Continue button to get the next block.
Example 2: Find the sum of squares of the first 100 numbers of the form prime minus one.
The line to type is:
Written by Dario Alpern. Last updated 2 March 2019.
If you find any error or you have a comment, please fill in the form.