Numbers
Numbers
In this doc we will talk about some interesting facts about numbers and how to deal with them in programming.
Basic Concepts
Prime Numbers
Prime numbers are numbers that are greater than 1 and have no divisors other than 1 and themselves. They are the building blocks of all other numbers and have many interesting properties.
质数是大于1且除了1和自身外没有其他因子的数。
How to check if a number is prime (ignore brute force method):
Method 1: Check divisibility up to the square root of the number.
1 |
|
More efficient method: Magic numbers 6k ± 1.
By observing the pattern of prime numbers, we can see that all prime numbers greater than 3 can be written in the form of 6k ± 1, where k is a positive integer.
So we can check the divisibility of the number by 6k ± 1 up to the square root of the number.
1 | extension Int { |
当我们要进行整数的除法检查时,需要将这个小数向上取整,以确保不遗漏任何可能的整数因数。如果直接转换为 Int,则会向下取整,可能会漏掉一个关键因数。
例如,n = 17:
- sqrt(17) 大约是 4.123,如果不取整,直接转换为 Int 得到的结果是 4。
- 然而,4 是不足够进行完整检查的,因为你实际上需要检查 5,以确保没有因数遗漏。
Common Swift Methods Used to Deal with Numbers
1. abs
abs returns the absolute value of a number.
1 | let a = -5 |
2. min and max
min and max return the minimum and maximum of two numbers.
1 | let a = 5, b = 10 |
3. round, ceil, and floor
round rounds a number to the nearest integer, ceil rounds up, and floor rounds down. They all return a Double value.
If you want to round a Double to an Int, you can use Int().
1 | let a = 5.6 |
4. isMultiple
isMultiple checks if a number is a multiple of another number.
This is same as number % anotherNumber == 0.
1 | let a = 10, b = 5 |
5. squareRoot
squareRoot returns the square root of a number.
1 | let a = 25 |
This is the same as sqrt(Double(a)).
Both sqrt and squareRoot return a Double value and the efficiency is almost the same. Choose one based on code style.