1. Multiples of 3 and 5 (1*)
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Method 1: Set, union, sum, print
Method 2: collect, println
union : union is still a set operation, union([1,2],[2,3])=[1,2,3], even though we apply it on arrays.
println: print line, print the argument and start a new line.
2 Even Fibonacci numbers (1*)
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Method 1 : while loop, append!, filter
while : while loop while condition body end
append!(collection, collection2): append collection 2 to collection, ! indicates collection will takes on the new value
last(coll): getting the last element of an ordered collection
iseven(x::Integer): test whether the integer x is even, and gives either 0 or 1 of type Bool
filter(f,a::AbstractArray): select elements from the array a base on the result of the boolean function f applying to each element of a
Method 2 : broadcasting
. : broadcasting, since A is an array, we want to broadcast the function iseven onto its elements
iseven.(A): gives an BitArray, with elements 0 or 1, e.g. iseven([1 2 3]) ----->[0 1 0]
A[ iseven.(A) ] : extract elements on positions with an 1 in the BitArray
$ : expression interpolation
3 Largest prime factor (1*)
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Method 1 : calling package
using package: packages can be called with the using command
factor(n::Integer) : returns the prime factorization of the integer, e.g. factor(-9) returns -1 ⋅ 3^2
collect(collection): turn our collection into an array, in this case, we get an array of pairs, (arraye，exponent).
Extra: package manager
We can enter the Julia package management mode, by enter ] in REPL
add package: add and install package automatically
rm package : remove a package
update : update a given package, or if no name is supplied, all packages will be updated
4 Largest palindrome product (1*)
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
a==b: checks if a=b, and return 1 or 0
We then create a multiplication table M , and broadcast the function isPalindrome to all entries in M, and select the Palindromes
maximum(itr): find the maximum element in itr
Method 2 : another way of defining a function
We can also define a function in the more "mathematical" fashion
5 Smallest Multiple (1*)
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Extra: Plots 1, Scatter
scatter: scatter plot, where the vertical axis is on a log scale
6 Sum Square Difference
Extra: Plots 2, plot, labels
I: gives the x coordinates
A,B : each gives a set of y coordinates
7 10001st prime
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
8 Largest product in a series
9 Special Pythagorean triplet
10 Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.