Arithmetic
arith factorialarith factorsarith frac_addarith frac_divarith frac_mularith frac_subarith gcdarith gcdarith iquoarith iremarith is_primearith lcmarith lcmarith ncrarith next_primearith nprarith to_fractionarith divisorsarith invmodarith isqrtarith phiarith powmod

arith factorial <n>


Description

    Get n! (exact, n <= 20000)

Parameters

    n:   An integer >= 0 - Number - required
admin
arith factorial 25
mentdb
15511210043330985984000000
50! exactly
admin
arith factorial 50
mentdb
30414093201713378043612608166064768844377641568960512000000000000
0! = 1
admin
arith factorial 0
mentdb
1
Exact value of 100!
admin
arith factorial 100
mentdb
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Number of orders of a deck of 52 cards
admin
arith factorial 52
mentdb
80658175170943878571660636856403766975289505440883277824000000000000

arith factors <n>


Description

    Get the prime factors (JSON array, with repetition). Pollard rho method: numbers up to about 60 digits.

Parameters

    n:   An integer >= 2 - Number - required
admin
arith factors 360
mentdb
[2,2,2,3,3,5]
admin
arith factors 18446744073709551617
mentdb
[274177,67280421310721]
A large number
admin
arith factors 1234567890
mentdb
[2,3,3,5,3607,3803]
Prime number
admin
arith factors 97
mentdb
[97]
Power of 2
admin
arith factors 1024
mentdb
[2,2,2,2,2,2,2,2,2,2]
Product of two large primes
admin
arith factors 600851475143
mentdb
[71,839,1471,6857]

arith frac_add <a> <b>


Description

    Add two fractions (exact)

Parameters

    a:   A fraction p/q, an integer or a decimal - String - required
    b:   A fraction p/q, an integer or a decimal - String - required
admin
arith frac_add "1/3" "1/6"
mentdb
1/2
admin
arith frac_add 0.5 "1/3"
mentdb
5/6
Sum of three fractions
admin
arith frac_add (arith frac_add "1/2" "1/3") "1/6"
mentdb
1
Negative fractions
admin
arith frac_add "-1/4" "1/6"
mentdb
-1/12
Integers and fractions
admin
arith frac_add 2 "3/5"
mentdb
13/5

arith frac_div <a> <b>


Description

    Divide two fractions (exact)

Parameters

    a:   A fraction p/q, an integer or a decimal - String - required
    b:   A fraction p/q, an integer or a decimal - String - required
admin
arith frac_div "1/2" "1/4"
mentdb
2
Division by an integer
admin
arith frac_div "3/4" 3
mentdb
1/4
Negative fraction
admin
arith frac_div "-2/3" "4/9"
mentdb
-3/2
Division by 0: error
admin
arith frac_div "1/2" 0
mentdb
Sorry, an error appears (division by zero).

arith frac_mul <a> <b>


Description

    Multiply two fractions (exact)

Parameters

    a:   A fraction p/q, an integer or a decimal - String - required
    b:   A fraction p/q, an integer or a decimal - String - required
admin
arith frac_mul "2/3" "9/4"
mentdb
3/2
Simplification
admin
arith frac_mul "4/6" "3/8"
mentdb
1/4
By an integer
admin
arith frac_mul "5/12" 6
mentdb
5/2
Recipe for 6 people from a recipe for 4: 3/4 cup x 6/4
admin
arith frac_mul "3/4" "6/4"
mentdb
9/8

arith frac_sub <a> <b>


Description

    Subtract two fractions (exact)

Parameters

    a:   A fraction p/q, an integer or a decimal - String - required
    b:   A fraction p/q, an integer or a decimal - String - required
admin
arith frac_sub "1/2" "3/4"
mentdb
-1/4
Same fraction: 0
admin
arith frac_sub "2/3" "4/6"
mentdb
0
Integer minus a fraction
admin
arith frac_sub 1 "5/8"
mentdb
3/8

arith gcd <a> <b>


Description

    Get the greatest common divisor of two integers

Parameters

    a:   An integer - Number - required
    b:   An integer - Number - required
admin
arith gcd 48 180
mentdb
12
Large numbers
admin
arith gcd 1071 462
mentdb
21
Coprime numbers: 1
admin
arith gcd 35 64
mentdb
1
Simplify 84/126
admin
arith gcd 84 126
mentdb
42
Large integers
admin
arith gcd 123456789012345678 987654321098765432
mentdb
2

arith gcd <json_array>


Description

    Get the greatest common divisor of a list of integers

Parameters

    json_array:   A JSON array of integers - String - required
admin
arith gcd "[12, 18, 30]"
mentdb
6
Gear teeth
admin
arith gcd "[24, 36, 60, 84]"
mentdb
12
Coprime numbers
admin
arith gcd "[6, 10, 15]"
mentdb
1

arith iquo <a> <b>


Description

    Get the quotient of the Euclidean division: a = b.q + r with 0 <= r < |b|

Parameters

    a:   An integer - Number - required
    b:   An integer (not 0) - Number - required
admin
arith iquo 17 5
mentdb
3
admin
arith iquo -17 5
mentdb
-4
Weeks in 100 days
admin
arith iquo 100 7
mentdb
14
Division by 0: error
admin
arith iquo 10 0
mentdb
Sorry, an error appears (division by zero).

arith irem <a> <b>


Description

    Get the remainder of the Euclidean division (always >= 0, unlike 'math mod')

Parameters

    a:   An integer - Number - required
    b:   An integer (not 0) - Number - required
admin
arith irem 17 5
mentdb
2
admin
arith irem -17 5
mentdb
3
Day of the week 100 days after a Monday (0 = Monday)
admin
arith irem 100 7
mentdb
2
Divisible: 0
admin
arith irem 144 12
mentdb
0

arith is_prime <n>


Description

    Check if an integer is prime (1 or 0)

Parameters

    n:   An integer (any size) - Number - required
admin
arith is_prime 97
mentdb
1
admin
arith is_prime 2305843009213693951
mentdb
1
A composite number
admin
arith is_prime 1001
mentdb
0
2 is prime
admin
arith is_prime 2
mentdb
1
1 is not prime
admin
arith is_prime 1
mentdb
0
Mersenne number 2^31 - 1
admin
arith is_prime 2147483647
mentdb
1

arith lcm <a> <b>


Description

    Get the least common multiple of two integers

Parameters

    a:   An integer - Number - required
    b:   An integer - Number - required
admin
arith lcm 4 6
mentdb
12
Two periods: when do they meet again?
admin
arith lcm 12 18
mentdb
36
Coprime numbers: the product
admin
arith lcm 7 9
mentdb
63

arith lcm <json_array>


Description

    Get the least common multiple of a list of integers

Parameters

    json_array:   A JSON array of integers - String - required
admin
arith lcm "[2, 3, 4, 5]"
mentdb
60
Common denominator of 1/4, 1/6 and 1/10
admin
arith lcm "[4, 6, 10]"
mentdb
60
Numbers 1 to 10
admin
arith lcm "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
mentdb
2520

arith ncr <n> <k>


Description

    Get the number of combinations C(n, k) (exact)

Parameters

    n:   An integer >= 0 - Number - required
    k:   An integer >= 0 - Number - required
admin
arith ncr 100 50
mentdb
100891344545564193334812497256
Lottery: 6 numbers among 49
admin
arith ncr 49 6
mentdb
13983816
C(n, 0) = 1
admin
arith ncr 10 0
mentdb
1
Symmetry: C(10, 3) = C(10, 7)
admin
arith ncr 10 7
mentdb
120
Hands of 5 cards among 52
admin
arith ncr 52 5
mentdb
2598960

arith next_prime <n>


Description

    Get the smallest prime number strictly greater than n

Parameters

    n:   An integer (any size) - Number - required
admin
arith next_prime 13
mentdb
17
After 1000
admin
arith next_prime 1000
mentdb
1009
After a prime number: the next one
admin
arith next_prime 97
mentdb
101
Large number
admin
arith next_prime 1000000000000
mentdb
1000000000039

arith npr <n> <k>


Description

    Get the number of arrangements A(n, k) = n!/(n-k)! (exact)

Parameters

    n:   An integer >= 0 - Number - required
    k:   An integer >= 0 - Number - required
admin
arith npr 5 2
mentdb
20
Podium of 3 among 10 runners
admin
arith npr 10 3
mentdb
720
All the orders of 5 objects: 5!
admin
arith npr 5 5
mentdb
120
Codes of 4 different digits
admin
arith npr 10 4
mentdb
5040

arith to_fraction <x>


Description

    Convert a number to a fraction.
- A fraction is reduced: 6/8 -> 3/4.
- A decimal with less than 10 significant digits is converted exactly: 0.33 -> 33/100.
- A decimal with 10 significant digits or more is considered as a rounded result: the first convergent of its continued fraction within max(half a unit of the last digit, 1e-12 relative) is returned.

Parameters

    x:   A decimal number or a fraction - String - required
admin
arith to_fraction 0.75
mentdb
3/4
admin
arith to_fraction 0.30000000000000004
mentdb
3/10
Repeating decimal
admin
arith to_fraction 0.3333333333333333
mentdb
1/3
Approximation of pi
admin
arith to_fraction 3.14159
mentdb
314159/100000
Negative number
admin
arith to_fraction -0.125
mentdb
-1/8
Integer
admin
arith to_fraction 3
mentdb
3
Decimal with many digits
admin
arith to_fraction 0.142857
mentdb
142857/1000000

arith divisors <n>


Description

    Get all the positive divisors (sorted JSON array)

Parameters

    n:   An integer >= 1 - Number - required
admin
arith divisors 360
mentdb
[1,2,3,4,5,6,8,9,10,12,15,18,20,24,30,36,40,45,60,72,90,120,180,360]
Divisors of 28 (a perfect number)
admin
arith divisors 28
mentdb
[1,2,4,7,14,28]
Prime number
admin
arith divisors 13
mentdb
[1,13]
Square number
admin
arith divisors 36
mentdb
[1,2,3,4,6,9,12,18,36]

arith invmod <a> <m>


Description

    Get the modular inverse of a modulo m (error if gcd(a, m) > 1)

Parameters

    a:   An integer - Number - required
    m:   The modulus (> 0) - Number - required
admin
arith invmod 3 11
mentdb
4
7 x 103 = 1 (mod 120): RSA key
admin
arith invmod 7 120
mentdb
103
Check: 3 x 4 mod 11 = 1
admin
arith irem 12 11
mentdb
1
Not coprime: no inverse
admin
arith invmod 4 8
mentdb
Sorry, an error appears (4 is not invertible modulo 8 (gcd = 4)).

arith isqrt <n>


Description

    Get the integer square root: the largest integer r with r^2 <= n

Parameters

    n:   An integer >= 0 - Number - required
admin
arith isqrt 99
mentdb
9
Exact integer square root of a large number
admin
arith isqrt 1000000000000000000000
mentdb
31622776601
Perfect square
admin
arith isqrt 144
mentdb
12
isqrt(0) = 0
admin
arith isqrt 0
mentdb
0

arith phi <n>


Description

    Get the Euler totient: the number of integers in [1, n] coprime with n

Parameters

    n:   An integer >= 1 - Number - required
admin
arith phi 36
mentdb
12
Euler phi of a prime: p - 1
admin
arith phi 97
mentdb
96
phi(1) = 1
admin
arith phi 1
mentdb
1
RSA: phi(61 x 53) = 60 x 52
admin
arith phi 3233
mentdb
3120

arith powmod <a> <b> <m>


Description

    Get a^b mod m (exact, any size; b < 0: power of the modular inverse)

Parameters

    a:   An integer - Number - required
    b:   The exponent (integer) - Number - required
    m:   The modulus (> 0) - Number - required
admin
arith powmod 2 1000 1000000007
mentdb
688423210
RSA style exponentiation
admin
arith powmod 65 17 3233
mentdb
2790
Fermat little theorem: a^(p-1) mod p = 1
admin
arith powmod 3 96 97
mentdb
1
RSA decryption of 2790 with d = 2753
admin
arith powmod 2790 2753 3233
mentdb
65