Sum of four cubes

  1. Alpertron
  2. Web applications
  3. Sum of four cubes
Actions
Functions

This Web application finds the decomposition of any integer number not congruent to 4 or 5 (mod 9) into a sum of four cubes.

Formulas

The applet uses the following formulas:

  • 6x = (x − 1)³ + (−x)³ + (−x)³ + (x + 1)³
  • 6x + 3 = x³ + (−x + 4)³ + (2x − 5)³ + (−2x + 4)³
  • 18x + 1 = (2x + 14)³ + (−2x − 23)³ + (−3x − 26)³ + (3x + 30)³
  • 18x + 7 = (x + 2)³ + (6x − 1)³ + (8x − 2)³ + (−9x + 2)³
  • 18x + 8 = (x − 5)³ + (−x + 14)³ + (−3x + 29)³ + (3x − 30)³
  • 54x + 20 = (3x − 11)³ + (−3x + 10)³ + (x + 2)³ + (−x + 7)³
  • 72x + 56 = (−9x + 4)³ + (x + 4)³ + (6x − 2)³ + (8x − 4)³
  • 108x + 2 = (−x − 22)³ + (x + 4)³ + (−3x − 41)³ + (3x + 43)³
  • 216x + 92 = (3x − 164)³ + (−3x + 160)³ + (x − 35)³ + (−x + 71)³
  • 270x + 146 = (−60x + 91)³ + (−3x + 13)³ + (22x − 37)³ + (59x − 89)³
  • 270x + 200 = (3x + 259)³ + (−3x − 254)³ + (x + 62)³ + (−x − 107)³
  • 270x + 218 = (−3x − 56)³ + (3x + 31)³ + (−5x − 69)³ + (5x + 78)³
  • 432x + 380 = (−3x + 64)³ + (3x − 80)³ + (2x − 29)³ + (−2x + 65)³
  • 540x + 38 = (5x − 285)³ + (−5x + 267)³ + (3x − 140)³ + (−3x + 190)³
  • 810x + 56 = (5x − 755)³ + (−5x + 836)³ + (9x − 1445)³ + (−9x + 1420)³
  • 810x + 164 = (3x − 26)³ + (−3x + 1)³ + (5x − 21)³ + (−5x + 30)³
  • 1080x + 380 = (−x − 1438)³ + (x + 1258)³ + (−3x − 4037)³ + (3x + 4057)³
  • 1620x + 1334 = (−5x − 3269)³ + (5x + 3107)³ + (−9x − 5714)³ + (9x + 5764)³
  • 1620x + 1352 = (−5x + 434)³ + (5x − 353)³ + (9x − 722)³ + (−9x + 697)³
  • 2160x + 362 = (−5x − 180)³ + (5x + 108)³ + (−6x − 149)³ + (6x + 199)³
  • 6480x + 794 = (−5x − 83)³ + (5x + 11)³ + (−6x − 35)³ + (6x + 85)³

If n = 596, 1892, 2324, 2756, 4052, 4484 (mod 6480) the following formula is used:

54x + 2 = (29484x² + 2211x + 43)³ + (−29484x² − 2157x − 41)³ + (9828x² + 485x + 4)³ + (−9828x² − 971x − 22)³

If n = 254, 902, 1442, 1874, 1982, 2414, 3062, 3494, 3602, 4034, 4142, 5114, 5222, 5654, 5762, 6302 (mod 6480) a method due to Demjanenko is used. Notice that the results can have hundreds of digits in this case.

In the remaining cases the number n is replaced by −n and then all solutions are multiplied by −1.

Expressions

You can enter numbers or numeric expressions in the input box including parentheses. The operations supported are:

  • + 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.
  • Ans: retrieves the last answer.
  • AND, OR, XOR, NOT for binary logic. The operations are done in binary (base 2). Positive (negative) numbers are prepended with an infinite number of bits set to zero (one).
  • SHL or <<: When b ≥ 0, a SHL b shifts a left the number of bits specified by b. This is equivalent to a × 2b. Otherwise, a SHL b shifts a right the number of bits specified by −b. This is equivalent to floor(a / 2b). Example: 5 SHL 3 = 40.
  • SHR or >>: When b ≥ 0, a SHR b shifts a right the number of bits specified by b. This is equivalent to floor(a / 2b). Otherwise, a SHR b shifts a left the number of bits specified by −b. This is equivalent to a × 2b. Example: -19 SHR 2 = -5.
  • n!: factorial (n must be greater than or equal to zero). Example: 6! = 6 × 5 × 4 × 3 × 2 = 720.
  • n!! ... !: multiple factorial (n must be greater than or equal to zero). It is the product of n times nk times n2k ... (all numbers greater than zero) where k is the number of exclamation marks. Example: 7!! = 7 × 5 × 3 × 1 = 105.
  • p#: primorial (product of all primes less than or equal to p). Example: 12# = 11 × 7 × 5 × 3 × 2 = 2310.
  • B(n): Probable prime immediately before n. Example: B(24) = 23.
  • F(n): Fibonacci number Fn from the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. where each element equals the sum of the previous two members of the sequence. Example: F(7) = 13.
  • L(n): Lucas number Ln = Fn-1 + Fn+1
  • N(n): Probable prime immediately after n. Example: N(24) = 29.
  • P(n): Unrestricted Partition Number (number of decompositions of n into sums of integers without regard to order). Example: P(4) = 5 because the number 4 can be partitioned in 5 different ways: 4 = 3+1 = 2+2 = 2+1+1 = 1+1+1+1.
  • Gcd(m,n, ...): Greatest common divisor of these integers. Example: GCD(12, 16) = 4.
  • Lcm(m,n, ...): Least common multiple of these integers. Example: LCM(12, 16, 24) = 48.
  • FloorDiv(m,n): integer part of the quotient of m divided by n. Examples: floordiv(10, 7) = 1 and floordiv(-10, 7) = -2.
  • Mod(m,n): value of m modulo the absolute value of n. Examples: Mod(10, 7) = 3 and Mod(-10, 7) = 4.
  • Modinv(m,n): inverse of m modulo n, only valid when m and n are coprime, meaning that they do not have common factors. Example: Modinv(3,7) = 5 because 3 × 5 ≡ 1 (mod 7)
  • Modpow(m,n,r): finds mn modulo r. Example: Modpow(3, 4, 7) = 4, because 34 ≡ 4 (mod 7).
  • Totient(n): finds the number of positive integers less than n which are relatively prime to n. Example: Totient(6) = 2 because 1 and 5 do not have common factors with 6.
  • Jacobi(m,n): obtains the Jacobi symbol of m and n. When the second argument is prime, the result is zero when m is multiple of n, it is one if there is a solution of x² ≡ m (mod n) and it is equal to −1 when the mentioned congruence has no solution.
  • Random(m,n): integer random number between m and n.
  • Abs(n): absolute value of n.
  • Sign(n): returns zero if n is zero, −1 if negative or 1 if positive.
  • IsPrime(n): returns zero if n is not probable prime, −1 if it is. Example: IsPrime(5) = −1.
  • Sqrt(n): Integer part of the square root of the argument.
  • Iroot(n,r): Integer r-root of the first argument. Example: Iroot(8, 3) = 2.
  • NumDigits(n,r): Number of digits of n in base r. Example: NumDigits(13, 2) = 4 because 13 in binary (base 2) is expressed as 1101.
  • SumDigits(n,r): Sum of digits of n in base r. Example: SumDigits(213, 10) = 6 because the sum of the digits expressed in decimal is 2+1+3 = 6.
  • RevDigits(n,r): finds the value obtained by writing backwards the digits of n in base r. Example: RevDigits(213, 10) = 312.

You can use the prefix 0x for hexadecimal numbers, for example 0x38 is equal to 56.

Configuration

You can change settings for this application by pressing the Config button when a factorization is not in progress. A new window will pop up where you can select different settings:

  • Digits per group: In order to improve readability, big numbers are separated by spaces forming groups of a fixed number of digits. With this input box, you can determine the number of digits in a group.
  • Hexadecimal output: If this checkbox is set, the numbers are shown in hexadecimal format instead of decimal, which is the common notation. To enter numbers in hexadecimal format, you will need to precede them by the string 0x. For instance, 0x38 = 56. The program shows hexadecimal numbers with monospaced font.
  • Keyboard: This enables the user to select between numeric or complete (alphanumeric) virtual keyboard. The virtual keyboard appears on the screen when the user selects an input box on touch screens.

The configuration is saved in your device, so when you start again the calculator, all settings remain the same.

Batch processing

Write an expression per line, then press the Sum if cubes 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 decompose several numbers as a sum of cubes by 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 cubes.
  • 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 decomposition in a sum of cubes 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 decomposition in a sum of cubes of the first 100 numbers of the form prime minus one. The line to type is: x=3;x=n(x);c<=100;x-1.

The fourth expression can be changed to a format string and several expressions. The format string indicates what is displayed. Inside this string you can specify conversion clauses that start by a percent sign and they are case insensitive:

  • %D: show expression as a decimal number.
  • %X: show expression as a hexadecimal number.
  • %L: show yes if the expression is not zero and no if it is zero.
  • %FD: decompose expression as a sum of powers decimal number.
  • %FX: show expression as a hexadecimal number.

The expressions are written after the string, separated by colons. There must be a colon also between the string and the first expression.

To print a percent sign, you have to write two percent signs, and a quote is represented by a percent sign followed by a quote.

Example 3: For each number between -100 and 100, show whether the number is prime and then the decomposition in cubes both in decimal and hexadecimal.

The line to write is: x=-100; x=x+1; x<=100; "%d is prime: %l, %Fd, %Fx":x:isprime(x):x:x

Source code

You can download the source of the current program and the old sum of four cubes applet from GitHub. Notice that the source code is in C language and you need the Emscripten environment in order to generate JavaScript.

Written by Dario Alpern. Last updated 30 August 2026.

Sum,+,Subtraction,-,Multiplication,*,Division,/,Remainder,%,Power,^,Last answer,ans,Integer square root,sqrt1,Integer root\n\nFirst argument: radicand\nSecond argument: root order,iroot2,Random number\n\nFirst argument: minimum value for random number\nSecond argument: maximum value for random number,Random2,Absolute value,Abs1,Sign,Sign1
Variable,x,Initialize variable,x=,Variable is less than,x<,,,Variable is less than or equal to,x<=,Counter,c,Counter is less than,c<,Counter is less than or equal to,c<=,Separator,;
Equal,=,Not equal,!=,,,Greater,>,Not greater,<=,Less,<,Not less,>=
Shift left\n\nLeft operand: value to shift\nRight operand: number of bits, SHL ,Shift right\n\nLeft operand: value to shift\nRight operand: number of bits, SHR ,,,Logic AND, AND ,Logic OR, OR ,Exclusive OR, XOR ,Logic NOT, NOT
,,Greatest Common Divisor\n\nOne or more arguments can be used,GCD2,Least Common Multiple\n\nOne or more arguments can be used,LCM2,The value is prime?,IsPrime1
Next prime after,N1,Last prime before,B1,,,Number of digits\n\nFirst argument: value\nSecond argument: base,NumDigits2,Sum of digits\n\nFirst argument: value\nSecond argument: base,SumDigits2,Reverse digits\n\nFirst argument: value\nSecond argument: base,RevDigits2
Integer part of quotient\n\nFirst argument: dividend\nSecond argument: divisor,FloorDiv2,Modulo\n\nFirst argument: value\nSecond argument: modulo,Mod2,Modular inverse\n\nFirst argument: value\nSecond argument: modulus,ModInv2,Modular division\n\nFirst argument: dividend\nSecond argument: divisor\nThird argument: modulus,ModDiv3,Modular power\n\nFirst argument: base\nSecond argument: exponent\nThird argument: modulus,ModPow3,Totient,Totient1,Jacobi symbol\n\nFirst argument: upper value\nSecond argument: lower value,Jacobi2
Factorial,!,,,Primorial,#,Fibonacci,F1,Lucas,L1,Partition,P1
Sum,+,Subtraction,-,Multiplication,*,Division,/,,,Hex prefix,0x,10,A,11,B,12,C,13,D,14,E,15,F
Left parenthesis,(,Right parenthesis,),New line,⏎,
Please type a number or expression.
This is the JavaScript version.
This is the WebAssembly version.
Press the Help button to get help about this application. Press it again to return to this screen. Keyboard users can press CTRL+ENTER to start calculation.
Computing sum of squares...

Calculation stopped by user

Done
Step 1 of 1: Expression to process
Step 1 of 5: Initial value of x
Step 2 of 5: Value of x for new iteration
Step 3 of 5: End loop condition
Step 4 of 5: Expression to process
Step 5 of 5: Process expression condition
Do not use variables x or c. Example for Smith numbers less than 10000: 1
Variables x and/or c required. Example for Smith numbers less than 10000:
sendmail