Integers: Number Line, Operations, Divisibility, GCD and LCM

Updated 19 min read Beginner to intermediate Baca dalam Bahasa Indonesia

Numbers Fundamentals Computing

Quick answer

The integers are the whole numbers and their negatives: …,-3,-2,-1,0,1,2,3,…, written ℤ. You can add, subtract and multiply any two of them and always get another integer, while division may leave a remainder: 17=5·3+2. Integers sit on a number line, split into primes and composites, and share factors measured by the GCD and the LCM.

Key takeaways

  • An integer is a whole number, positive, negative or zero; zero is neither positive nor negative, and every integer a has an opposite -a with a+(-a)=0.
  • On the number line the number further right is greater, so -7<-3; the absolute value |a| is the distance from 0.
  • Subtracting is adding the opposite (5-(-3)=8), and a product or quotient is negative exactly when the signs differ.
  • Divisibility rules test 2, 3, 4, 5, 9, 10 and 11 from the digits alone; every integer above 1 is a product of primes in exactly one way.
  • Euclid's algorithm finds the GCD by repeated division with remainder, and \gcd(a,b)·\operatornamelcm(a,b)=a· b.
  • With negative numbers, languages disagree about remainders: -7\bmod3 is 2 in Python and -1 in JavaScript.

What are integers?

Integers are the numbers with no fractional part: the counting numbers 1,2,3,…, their negatives -1,-2,-3,… and zero. The set is written ℤ, from the German Zahlen ("numbers").

Negative numbers exist because people need to describe things that go below a starting point, and the integers are what you get when a quantity can go either way:

SituationPositiveZeroNegative
Temperature (°C)25 above freezingfreezing point−5 below freezing
Height8 m above sea levelsea level−12 m, a diver
Lift buttonsfloor 3ground floor−1, a basement
Money50 dollars in the accountnothing−30, an overdraft
Goal difference2 goals aheadlevel−3, behind

Three kinds of number are often confused. The natural numbers are 1,2,3,… (here they start at 1; some books start at 0). The whole numbers are 0,1,2,3,…. The integers add the negatives. Every natural number is a whole number and every whole number is an integer, but not the other way round; the article on real numbers shows how all these sets nest.

Two facts about zero and signs are worth fixing now. Zero is neither positive nor negative. And every integer a has an opposite, -a, on the other side of 0 at the same distance, so that a+(-a)=0. The opposite of 7 is -7, the opposite of -7 is 7, and the opposite of 0 is 0, which is why -(-a)=a.

The integers are a closed family under three operations: add, subtract or multiply two integers and the result is always an integer. They are not closed under division, since 7÷2 is not an integer. That one gap is what the later sections on remainders are about, and what the article on rational numbers closes with fractions.

Try it: which are integers?

Choose all the integers.

  • 5
  • -3
  • 0
  • 2.5
  • \frac12
  • (10)/(5)

How do you compare integers and what is absolute value?

On the number line the integer further to the right is the greater one, so -3>-7 even though 7 is bigger than 3. Every positive integer is greater than 0 and every negative one is less than 0, which makes every positive integer greater than every negative one.

To compare two negative numbers, compare their sizes and reverse the answer: 7>3, so -7<-3. A debt of 7 is worse than a debt of 3.

The absolute value |a| is the distance from a to 0, so it is never negative: |7|=7, |-7|=7 and |0|=0. In symbols, |a|=a when a\ge0 and |a|=-a when a<0. Opposites have the same absolute value, and the distance between two integers is the absolute value of their difference: |-4-3|=7.

StatementTrue?Why
-3>-7yes-3 is to the right of -7
-1<0yesevery negative is less than 0
|-5|=|5|yesboth are 5 away from 0
-9<-10no-9 is to the right of -10

Ordering a list is a matter of reading it left to right on the line: negatives first, with the largest size furthest left, then 0, then the positives.

Try it: order from least to greatest

Put these integers in order from least to greatest.

How do you add and subtract integers?

To add integers, move along the number line: adding a positive moves right and adding a negative moves left; to subtract, add the opposite, so a-b=a+(-b). That one rule covers every case.

Two rules for adding, by the signs:

  • Same signs: add the sizes and keep the sign. 5+3=8 and (-5)+(-3)=-8.
  • Different signs: subtract the smaller size from the larger, and keep the sign of the larger. 5+(-8)=-3, because 8 is the larger size and it is negative. (-5)+8=3.

Two numbers that are opposites cancel: 6+(-6)=0.

Subtracting is adding the opposite. Taking away a positive moves left: 2-5=2+(-5)=-3. Taking away a negative moves right, because removing a debt makes you richer: 5-(-3)=5+3=8 and (-5)-(-3)=-5+3=-2.

ExpressionRewrittenResult
5-(-3)5+38
-5-3-5+(-3)−8
-5-(-3)-5+3−2
-5+8different signs, 8 is larger3

The order does not matter for adding, a+b=b+a, but it does for subtracting: 5-3=2 while 3-5=-2. Try your own numbers below; the first jump (above the line) goes from 0 to a and the second (below) is the change.

Interactive: add and subtract on the number line

Move two sliders and watch the sum or difference of two integers drawn as two jumps on a number line, with the rule that applies.

Try it: add and subtract

Evaluate.

How do you multiply and divide integers?

Multiply or divide the sizes, then make the answer positive if the signs are the same and negative if they differ. A product with a factor of 0 is 0.

FirstSecondProduct or quotientExample
positivepositivepositive4·3=12
negativenegativepositive(-4)(-3)=12
positivenegativenegative4·(-3)=-12
negativepositivenegative(-4)·3=-12

Division follows the same signs, because a÷ b=c means a=b· c: (-12)÷4=-3 since 4·(-3)=-12, and (-12)÷(-3)=4 since (-3)·4=-12.

Why is a negative times a negative positive? Two arguments show it is not a convention but forced.

A pattern. Count down the first factor and watch the products: 3·(-2)=-6, 2·(-2)=-4, 1·(-2)=-2, 0·(-2)=0. Each step down adds 2. The next step must add 2 again: (-1)·(-2)=2.

The distributive law. Start from 0=(-1)·0=(-1)\bigl(1+(-1)\bigr)=(-1)·1+(-1)(-1)=-1+(-1)(-1). For the right side to equal 0, (-1)(-1) has to be 1. Any other value would break the distributive law, a(b+c)=ab+ac, that the whole of arithmetic depends on.

Division by zero is undefined. a÷0=c would mean a=0· c=0, which is impossible for a≠0; and 0÷0 could be any number, so it has no single value. Meanwhile 0÷ a=0 for a≠0.

Integers are not closed under division: 7÷2 is the fraction \frac72, which is why division with remainder is its own topic below.

Interactive: the four operations on integers

Type two integers, even very large ones, pick an operation and see the exact result and the sign rule behind it.

Try it: multiply and divide

Evaluate.

What is the order of operations with negative numbers?

Work in this order: brackets, then powers, then multiplication and division from left to right, then addition and subtraction from left to right. Negative numbers add two traps to it.

  1. A minus sign is not a bracket. -3^2 means -(3^2)=-9, because the power binds tighter than the minus. Only (-3)^2=9 squares the -3.
  2. Same-level operations go left to right. 12÷(-3)×2 is -4×2=-8, not 12÷(-6)=-2. And 10-4-3=3, not 10-(4-3)=9.
  3. Put brackets round a negative after an operator. 5·-2 is written 5·(-2), and 8-3×(-2)=8+6=14: the multiplication goes before the subtraction.
ExpressionStepsResult
-3^2-(9)−9
(-3)^299
8-3×(-2)8-(-6)=8+614
12÷(-3)×2-4×2−8
-2 (3-7)-2·(-4)8

An even power of a negative number is positive and an odd power is negative: (-2)^4=16 and (-2)^3=-8, since each pair of minus signs cancels.

Try it: order of operations

Evaluate.

What are even and odd numbers and the divisibility rules?

An integer a is divisible by b when a=b· k for some integer k: b is then a divisor (factor) of a and a is a multiple of b. So 12 is divisible by 4 because 12=4·3, and 0 is divisible by every integer because 0=b·0.

Even and odd. An even integer is divisible by 2, a=2k; an odd one is a=2k+1. Zero is even, and negative numbers have parity too: -3 is odd and -4 is even. The even numbers 2,4,6,…, the odd numbers 1,3,5,… and the multiples of any integer each form an arithmetic sequence, and their sums have a formula there. Parity follows a short table:

OperationResult
even + even, odd + oddeven
even + oddodd
even × anythingeven
odd × oddodd

A proof of one row: (2a+1)+(2b+1)=2(a+b+1), which is even. The same style of proof shows that the square of an odd number is odd, the fact behind the irrationality of √2.

Divisibility rules let you test a number from its digits, without dividing:

ByThe number is divisible whenWhy it works
2the last digit is even10 is a multiple of 2
3the digit sum is divisible by 310, 100, ... each leave remainder 1
4the last two digits form a multiple of 4100 is a multiple of 4
5the last digit is 0 or 510 is a multiple of 5
6it is divisible by both 2 and 32 and 3 share no factor
8the last three digits form a multiple of 81000 is a multiple of 8
9the digit sum is divisible by 910, 100, ... each leave remainder 1
10the last digit is 0the definition of base ten
11the alternating digit sum is divisible by 1110 is one less than a multiple of 11

Example: 7200 has digit sum 9 and ends in 00, so it is divisible by 2, 3, 4, 5, 6, 8, 9 and 10. For 11 take the digits from the right with alternating signs, 0-0+2-7=-5, which is not a multiple of 11, so 7200 is not. Try any number below, including a very long one.

Interactive: divisibility rules

Type any integer and see which of 2, 3, 4, 5, 6, 8, 9, 10 and 11 divide it, what each rule looks at, and the real remainder as a check.

Try it: a missing digit

The three-digit number 52d is divisible by 9. Find the digit d.

What are prime numbers and prime factorization?

A prime number is an integer greater than 1 whose only positive divisors are 1 and itself; every integer greater than 1 is either prime or a product of primes, and that product is unique. The primes begin 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47, fifteen of them below 50.

  • Composite numbers have more divisors: 12=3·4.
  • 1 is neither prime nor composite. It has one divisor, itself. Calling it prime would break uniqueness, since 6=2·3=1·2·3.
  • 2 is the only even prime, because every other even number is also divisible by 2.

The fundamental theorem of arithmetic says every integer greater than 1 can be written as a product of primes in exactly one way, apart from the order. So 360=2·2·2·3·3·5=2^3·3^2·5, and no other set of primes multiplies to 360.

To factorize, divide by the smallest prime that goes in, again and again, until 1 is left: 360÷2=180, ÷2=90, ÷2=45, ÷3=15, ÷3=5, ÷5=1.

To test whether n is prime, divide by the primes up to √n. If n had a factor larger than √n, its partner factor would be smaller than √n, so you would have met it already. Take 91: √(91)<10, and 91=7·13, so it only looks prime. For 97 nothing in 2, 3, 5, 7 divides it, so 97 is prime.

There are infinitely many primes. Euclid's argument: suppose a list of primes were complete, multiply them all and add 1. The result leaves remainder 1 on division by every prime on the list, so it either is a new prime or has one, and the list was not complete.

Divisors from the factorization. If n=p^a q^b… it has (a+1)(b+1)… positive divisors, so 360 has 4·3·2=24. Use the tool below.

Interactive: prime factorization

Type a whole number up to a trillion and see whether it is prime, its prime factorization step by step, and how many divisors it has.

Try it: spot the prime

Which of these is prime?

  • 51
  • 57
  • 91
  • 97

How do you find the GCD and LCM?

The greatest common divisor (GCD, in Indonesian FPB) of two integers is the largest integer that divides both, and the least common multiple (LCM, KPK) is the smallest positive integer that both divide. For 48 and 180 the GCD is 12 and the LCM is 720.

By prime factors. Write each number as primes: 48=2^4·3 and 180=2^2·3^2·5. For the GCD take each prime with the smaller exponent: 2^2·3=12. For the LCM take each prime with the larger exponent: 2^4·3^2·5=720.

By Euclid's algorithm, which needs no factoring and works for huge numbers. Divide, keep the remainder, and repeat with the divisor and the remainder until the remainder is 0; the last non-zero remainder is the GCD:

180=3·48+36, then 48=1·36+12, then 36=3·12+0. So \gcd(48,180)=12.

It works because any number dividing a and b also divides a-qb, and the other way round, so \gcd(a,b)=\gcd(b, a-qb) while the numbers shrink to zero. Euclid wrote it down in Book VII of the Elements, around 300 BCE, which makes it one of the oldest algorithms still in daily use.

The two are linked: \gcd(a,b)·\operatornamelcm(a,b)=a· b. Check: 12·720=8640=48·180. So \operatornamelcm(a,b)=(a· b)/(\gcd(a,b)). Two numbers are coprime when their GCD is 1, as with 8 and 15.

What they are for.

  • Simplifying a fraction: divide top and bottom by the GCD. (48)/(180)=(48÷12)/(180÷12)=(4)/(15).
  • Cutting into equal pieces: a floor 48 dm by 180 dm is tiled with the largest square tiles whose side is \gcd=12 dm, which takes 4·15=60 tiles.
  • Repeating events: buses leave every 12 and every 18 minutes, together at 7:00. They next leave together after \operatornamelcm(12,18)=36 minutes.
  • Adding fractions: the least common denominator is the LCM of the denominators.

Interactive: GCD and LCM with Euclid’s algorithm

Enter two positive integers and see Euclid’s algorithm run row by row, then the gcd, the lcm and the prime exponents behind them.

Try it: GCD and LCM

Find the GCD and the LCM of 36 and 48.

What is division with remainder and modular arithmetic?

Dividing an integer a by b≠0 gives a unique quotient q and remainder r with a=b· q+r and 0\le r<|b|; the remainder is written a\bmod b. For example 17=5·3+2, so 17\bmod5=2.

With a negative dividend the rule still holds, and the remainder stays between 0 and |b|-1: -7=3·(-3)+2, so -7\bmod3=2, not -1. Think of a clock: 7 hours before 12 o'clock is 5 o'clock, and a day of 24 hours repeats after 24, which is the same as counting remainders.

Modular arithmetic works with remainders only. Two integers are congruent modulo n, written a\equiv c\pmod n, when n divides a-c, that is, when they leave the same remainder: 17\equiv2\pmod5. Remainders respect addition and multiplication, so you can reduce first and calculate afterward:

  • Days of the week. 100 days after a Monday: 100\bmod7=2, so it is a Wednesday.
  • Last digits. The last digit of 7^k repeats 7, 9, 3, 1. Since 2026\bmod4=2, the last digit of 7^2026 is the second in the cycle, 9.
  • Parity. Even and odd are just a\bmod2.
  • Check digits and hashing. Barcodes and card numbers carry a digit computed with \bmod10 to catch typing errors, and a hash table picks a slot with \bmod the table size.

The catch is that programming languages do not all agree on negative operands. Each satisfies a=b· q+r but chooses q differently: mathematics keeps r\ge0; Python rounds q down, so r takes the sign of the divisor; JavaScript, C, C++ and Java truncate q toward zero, so r takes the sign of the dividend. Compare them below.

Interactive: division with remainder

Divide one integer by another and compare the quotient and remainder that mathematics, Python and JavaScript each give, including for negative numbers.

Try it: quotient and remainder

Write -17=5q+r with 0\le r<5.

How do you work with integers in Python and JavaScript?

Python integers have no size limit and use floor division, while JavaScript numbers are floating-point numbers (floats), exact only up to 2^53-1, with a remainder that follows the dividend; use BigInt in JavaScript for large exact integers. The two languages disagree exactly where this article has been pointing. Why fixed-size integers wrap around is explained by two’s complement in the article on binary numbers.

Python
>>> 7 // 2, -7 // 2          # floor division rounds down
(3, -4)
>>> 7 % 3, -7 % 3, 7 % -3        # the remainder takes the sign of the divisor
(1, 2, -2)
>>> divmod(-7, 3)                # quotient and remainder together
(-3, 2)
>>> int(-7 / 2)                  # / gives a float, int() truncates toward zero
-3
>>> import math
>>> math.gcd(48, 180), math.lcm(4, 6)
(12, 12)
>>> 2 ** 100                     # no overflow
1267650600228229401496703205376
Euclid’s algorithm and factorization in Python
def gcd(a, b):
    while b:
        a, b = b, a % b
    return abs(a)

def factorize(n):
    out, p = [], 2
    while p * p <= n:
        while n % p == 0:
            out.append(p)
            n //= p
        p += 1
    if n > 1:
        out.append(n)
    return out

gcd(48, 180)      # 12
factorize(360)    # [2, 2, 2, 3, 3, 5]
JavaScript
-7 % 3                          // -1   the remainder takes the sign of the dividend
((-7 % 3) + 3) % 3              // 2    the usual fix for a non-negative remainder
Math.trunc(-7 / 2)              // -3
Math.floor(-7 / 2)              // -4
Number.MAX_SAFE_INTEGER         // 9007199254740991
2 ** 53 + 1 === 2 ** 53         // true: beyond 2^53 integers lose exactness
2n ** 100n                      // 1267650600228229401496703205376n
-7n % 3n                        // -1n  BigInt behaves like Number here
7n / 2n                         // 3n   BigInt division truncates
C++
#include <iostream>
using namespace std;

int mod(int a, int b) { return ((a % b) + b) % b; }       // a non-negative remainder, for b > 0
int floorDiv(int a, int b) { return (a - mod(a, b)) / b; }
int gcd(int a, int b) {                                    // Euclid's algorithm
  while (b != 0) { int t = a % b; a = b; b = t; }
  return a < 0 ? -a : a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  cout << 7 / 2 << " " << 7 % 3 << endl;                   // 3 1
  // C++ truncates toward zero: -7 / 2 is -3 and -7 % 3 is -1
  cout << floorDiv(-7, 2) << " " << mod(-7, 3) << endl;    // floor division and the usual remainder
  cout << gcd(48, 180) << " " << lcm(4, 6) << endl;
  int n = 360;                                             // factorization by trial division
  for (int p = 2; p * p <= n; p++)
    while (n % p == 0) { cout << p << " "; n /= p; }
  if (n > 1) cout << n;
  cout << endl;
  return 0;
}

/* output:
3 1
-4 2
12 12
2 2 2 3 3 5
*/

The pitfalls, in order of how often they bite:

PitfallWhat happensWhat to do
-7 % 3 in JavaScriptgives −1, not 2use ((n % m) + m) % m when you need 0 to m − 1
7 / 23.5 in both languages; JavaScript has no integer divisionuse // in Python, Math.trunc or Math.floor in JavaScript
Python // with negativesrounds down: -7 // 2 is −4, not −3use int(-7 / 2) if you want truncation
Integers above 2⁵³ in JavaScriptsilently inexactuse BigInt and the n suffix; do not mix it with Number
Fixed-size integers in C and Java2147483647 + 1 wraps to −2147483648 in Java, and is undefined in Cchoose a wider type, or check before adding
Bitwise operators in JavaScript1 << 32 is 1: they work on 32 bitsavoid them for large values
0 % n and n % 00 is fine; dividing by 0 raises an error in Python and gives NaN in JavaScriptcheck the divisor first

Python's math.gcd and math.lcm (from Python 3.9) take any size of integer, so you rarely need your own, but writing Euclid's loop once is the best way to see why it ends.

What are the common mistakes with integers?

The most common mistakes with integers are the nine below, each with the correct statement and a number that shows why.

MistakeCorrect
❌ -3^2=9-3^2=-(3·3)=-9. Only (-3)^2=9.
❌ 5-(-3)=25-(-3)=5+3=8. Subtracting a negative adds.
❌ -5-3=-2-5-3=-5+(-3)=-8.
❌ (-2)(-3)=-6Same signs give a positive: 6.
❌ -7>-3 because 7>3The order reverses for negatives: -7<-3.
❌ 0 is a positive integer0 is neither positive nor negative.
❌ 1 is a prime number1 has one divisor; the primes start at 2.
❌ 5÷0=0Division by zero is undefined.
❌ \operatornamelcm and \gcd swappedGCD divides both numbers and is the smaller; the LCM is a multiple of both and is the larger: \gcd(12,18)=6, \operatornamelcm=36.

Practice: test your understanding

These questions mix everything above. A wrong answer costs nothing here: read the hint and try again.

True or false?

Decide whether each statement is True or False.

  • The sum of two negative integers is negative.
  • -7>-3.
  • 0 is a positive integer.
  • (-4)×(-5)=20.
  • 1 is a prime number.

Select all that apply

Choose all the numbers divisible by both 3 and 4.

  • 24
  • 18
  • 36
  • 100
  • 72

Add and subtract

Evaluate.

A greatest common divisor

Find the GCD of 84 and 126.

A remainder in JavaScript

What does the JavaScript expression ´-17 % 5´ give?

  • 3
  • -2
  • 2
  • -3

A diver

A diver is at -12 m and swims up 5 m. Where is the diver now?

  • -7 m
  • -17 m
  • 7 m
  • 17 m

Summary: integers at a glance

  • Integers: …,-2,-1,0,1,2,…; zero is neither positive nor negative; every a has an opposite -a.
  • Order and size: further right is greater, so -7<-3; |a| is the distance from 0.
  • Adding and subtracting: same signs add and keep the sign; different signs subtract and keep the sign of the larger; a-b=a+(-b).
  • Multiplying and dividing: same signs give positive, different signs give negative; never divide by 0.
  • Order of operations: brackets, powers, then ×÷, then +− left to right; -3^2=-9 but (-3)^2=9.
  • Divisibility: digit rules for 2, 3, 4, 5, 6, 8, 9, 10, 11; every integer above 1 has one prime factorization.
  • GCD and LCM: smaller or larger prime exponents, or Euclid's algorithm; \gcd·\operatornamelcm=a· b.
  • Remainders: a=bq+r with 0\le r<|b|; Python, JavaScript and mathematics differ for negatives.

Glossary: key terms

Integer
A number in the set ℤ, with no fractional part: zero, the positive whole numbers and their negatives.
Opposite
The number on the other side of zero at the same distance, so that a number plus its opposite is zero.
Absolute value
The distance of a number from zero, which is never negative, so the absolute value of minus 7 is 7.
Closed under an operation
A set is closed under an operation when applying it to members of the set always gives a member; the integers are closed under addition, subtraction and multiplication.
Divisor (factor)
An integer b is a divisor of a when a equals b times some integer, so that division leaves no remainder.
Multiple
The product of a number and any integer, such as 12, 24 and 36 for the number 12.
Prime number
An integer greater than 1 whose only positive divisors are 1 and itself.
Composite number
An integer greater than 1 that is not prime, so it has a divisor other than 1 and itself.
Prime factorization
The unique way of writing an integer greater than 1 as a product of primes, such as 360 equal to 2 cubed times 3 squared times 5.
Greatest common divisor (GCD)
The largest integer that divides both of two given integers; for 48 and 180 it is 12.
Least common multiple (LCM)
The smallest positive integer that is a multiple of both of two given integers; for 12 and 18 it is 36.
Coprime
Two integers whose greatest common divisor is 1, such as 8 and 15.
Remainder
What is left after dividing: in a equal to b times q plus r, the number r, which lies from 0 up to the size of b minus 1.
Modulo
The operation that gives the remainder of a division, so that 17 modulo 5 is 2.

Frequently asked questions

What is an integer?

An integer is a number with no fractional part: zero, the positive whole numbers 1, 2, 3 and so on, and their negatives minus 1, minus 2, minus 3 and so on. The set of integers is written with the letter Z.

Is zero a positive or a negative integer?

Zero is neither positive nor negative. It is an integer, it is even, and it is the dividing point on the number line between the negative integers on the left and the positive integers on the right.

What is the difference between natural numbers, whole numbers and integers?

Natural numbers are 1, 2, 3 and so on. Whole numbers add zero. Integers add the negative numbers as well. So every natural number is a whole number, and every whole number is an integer, but not the other way round.

Why is a negative times a negative positive?

Because the distributive law forces it. Since minus 1 times the sum of 1 and minus 1 is zero, minus 1 plus minus 1 times minus 1 must be zero, so minus 1 times minus 1 has to be 1. Any other value would break ordinary arithmetic.

How do you subtract a negative number?

Subtracting a negative number is the same as adding its opposite. So 5 minus minus 3 is 5 plus 3, which is 8. On the number line you move to the right, just as taking away a debt leaves you better off.

What is absolute value?

The absolute value of a number is its distance from zero on the number line, so it is never negative. The absolute value of 7 and of minus 7 is 7, and the absolute value of zero is zero. It is written with two vertical bars.

Why can you not divide by zero?

Dividing a by zero would need a number c with 0 times c equal to a. Zero times anything is zero, so this is impossible when a is not zero, and when a is zero every c would work. Either way there is no single answer.

How do you know if a number is divisible by 3 or 9?

Add up its digits. If the digit sum is divisible by 3, so is the number, and if the digit sum is divisible by 9, so is the number. For example 522 has digit sum 9, so it is divisible by both 3 and 9.

Is 1 a prime number?

No. A prime has exactly two positive divisors, 1 and itself, while 1 has only one divisor. Excluding it keeps prime factorization unique, because otherwise 6 could be written as 2 times 3 and also as 1 times 2 times 3. The first prime is 2.

How do you check whether a number is prime?

Try dividing it by the primes up to its square root. If none divides it, it is prime. You can stop at the square root because any larger factor would pair with a smaller one you already tried. So 97 is prime, while 91 equals 7 times 13.

How do you find the GCD of two numbers?

Either take each shared prime factor with its smaller exponent, or use Euclid’s algorithm: divide the larger number by the smaller, replace the pair by the smaller number and the remainder, and repeat. The last non-zero remainder is the GCD.

What is the difference between GCD and LCM?

The GCD is the largest number that divides both numbers, so it is never larger than either. The LCM is the smallest number that both divide, so it is never smaller than either. Their product equals the product of the two numbers.

What is the remainder when a negative number is divided?

In mathematics the remainder is never negative, so minus 7 divided by 3 leaves 2, because minus 7 is 3 times minus 3 plus 2. Python agrees, but JavaScript, C and Java return minus 1, because they cut the quotient toward zero.

How do you do integer division in Python?

Use the double slash operator, which rounds down, so 7 // 2 is 3 and minus 7 // 2 is minus 4. A single slash always gives a float. The percent sign gives the remainder, and divmod returns the quotient and the remainder together.

References

  1. James Stewart, Lothar Redlin and Saleem Watson (2016). Precalculus: Mathematics for Calculus (7th ed.). Cengage Learning.
  2. Euclid. Elements, Book VII (the Euclidean algorithm) and Book IX, proposition 20 (there are infinitely many primes). c. 300 BCE.
  3. Brahmagupta (628). Brahmasphutasiddhanta (rules for arithmetic with zero, debts and fortunes).
  4. Carl Friedrich Gauss (1801). Disquisitiones Arithmeticae.
  5. Ronald Graham, Donald Knuth and Oren Patashnik (1994). Concrete Mathematics: A Foundation for Computer Science (2nd ed.), chapter 3 (floor, ceiling and mod). Addison-Wesley.
  6. Shen Kangshen, John Crossley and Anthony Lun (1999). The Nine Chapters on the Mathematical Art: Companion and Commentary. Oxford University Press.
  7. Python Software Foundation. The Python Language Reference: binary arithmetic operations (floor division and modulo). docs.python.org. https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations.
  8. Ecma International. ECMAScript Language Specification: multiplicative operators (the remainder operator). tc39.es. https://tc39.es/ecma262/#sec-multiplicative-operators.