In [18]:
function fill_twos(a)
for i =1:length(a)
a[i]=2
end
end
function strange_twos(n)
a=Array(rand(Bool) ? Int64 : Float64,n)
fill_twos(a)
print(a)
end
strange_twos(10)
In [17]:
x=[1 2 ; 3 4]
print(x)
In [21]:
##Project _Euler 1:
## Description: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.
function getMul(a,n)
##a is the array, n is the number upto which we need the sum ##
i,j=0,0
li=[]
for j in 1:n
if j%a[1] == 0 || j%a[2]==0
push!(li,j)
end
end
return li
end
function getSum(li)
sum=0
for i in li
if i < li[length(li)]
sum+=i
end
end
return sum
end
function main(a,n)
li=getMul(a,n)
sum=getSum(li)
@show(sum)
end
println("::the Project euler problem 1::")
main([3,5],1000)
Out[21]:
In [4]:
##Project _Euler 2:
## Description: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.
function getFib(k)
#n is the limit of the fibonacci series...
a=[]
i=0
fib(n)=n<2 ? n : fib(n-1) + fib(n-2)
while true
i+=1
m=fib(i)
if m<k
push!(a,m)
else
return a
end
end
end
function getFibEvenSum(n)
li=getFib(n)
sum=0
for i in li
if i%2 ==0
sum+=i
end
end
return sum
end
function main()
println(getFibEvenSum(4000000))
end
println("::the Project euler problem 2::")
main()
In [10]:
##Project _Euler 3:
## Description:The prime factors of 13195 are 5, 7, 13 and 29.
##What is the largest prime factor of the number 600851475143 ?
function getFactor(num)
a=[]
for i in 1:num
if num%i ==0
push!(a,i)
end
end
return a ### returns the factors of the given numbers.
end
function getPrimeNum(li)
cnt=0
l=[]
for i in li
for j in 2:i-1
if i%j != 0
cnt+=1
end
end
if cnt == 0
push!(l,i)
end
end
return l ## returns the prime numbers in the given list.
end
function main()
li=getFactor(600851475143)
p=getPrimeNum(li)
larg=maximum(p)
println(large)
end
println("::the Project euler problem 3::")
main()
In [27]:
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
for i in 1:10 println(fib(i)) end