let s = [(a,b) | a<-[10..99], b<-[a+1..99], a*mod b 10 == b*div a 10, a`mod`10==b`div`10]There are only 4 entries so this can be done by hand :)
Sunday, 25 October 2009
#33
This can be found using a simple list comprehension. Just then multiply and collect.
#28
This problem can be solved by thought rather than brute force. Looking at the diagonals as 4 separate sums, the four diagonals form four number sequences which when summed give the value of the kth ring to be 4[(2k+1)^2-3k] (k>=1, k=0 is the first 1x1 grid).
Determining the link betweem the grid size and k was also trival, the ring size is 2k+1. Hence for a 1001x1001 grid, k = 500. We can simply sum the answer over t=1..k+1 to get the solution, and add one for the central 1.
Determining the link betweem the grid size and k was also trival, the ring size is 2k+1. Hence for a 1001x1001 grid, k = 500. We can simply sum the answer over t=1..k+1 to get the solution, and add one for the central 1.
let fk k = 4*((2*k+1)^2-3*k) let s = 1 + (sum . map fk $ [1..500])
#29
This problem can be solved trivially in haskell with a single line or so if you use Sets
let s = Data.Set.fromList [a^b | a<-[2..100], b<-[2..100]And then we just take the set size.
Friday, 16 October 2009
#25
There is no need for any code here as you can do it by math only. Given then we know formula for the fibonacci numbers is Fn = (1/sqrt5) * (phi^n + phi^-n), we can set Fn = 10^999 and then use logarithms to solve for n. (Where of course phi = (1+sqrt(5))/2).
n = (ln(sqrt5) + 1000ln(10)) / ln (phi)This will give us the "n" required to be just exactly equal to that number, we need to round and we get the answer.
#16
We can solve this one by converting 2^1000 into a string of digits and then sum it.
sum [read[n]::Int|n<-show(2^1000)]
#5
No code needed in this one, we just need to factor all of 1..20 and multiple so that the maximum power of each prime factor is in our answer. Only 2 primes appear at a power.
#4
Problem 4 is simple to naively code up and you could get something like the following.
reverse' :: Int -> Int reverse' n = read (reverse (show n)) -0 palindrome :: Int-> Bool palindrome n = (reverse' n == n) palins n m = [(a,b,a*b) | a<- [n..m], b<- [n..m], a<=b,palindrome (a*b)]Given this you can then run a maximum over the 3rd element of the tuplet. This isnt that quick for the bounds of [100..999]; so i did a bit of guess work and try [899..999]. This gave a sensible answer for which i was pretty confident would be right (and it was).
#6
Solving Problem 6 is a rather simple using list comprehension. The result is based that the sum squared is equal to the sum of the squares plus twice the cross terms.
let x n = sum [a*b | a <- [1..n], b <- [1..n], a/=b]
#2
Solving euler project #2 via haskell was a good challenge. Here is the modified solution once I found you can take each 3 in the fibonacci sequence instead of checking for even.
naturals n = n : naturals (n+1)
fib :: Integer→ Integer
fib n = round((phi↑n - (1-phi)↑n) / sqrt 5)
where phi = (1+sqrt 5)/2
sum' = sum (takeWhile (<4000000) (map fib (map (λx→ x*3) (naturals 1))))
Subscribe to:
Posts (Atom)