This following is my attempt using Clojure. It’s pretty blitzing quick.
; Determines the prime-factors of a natural number
(defn prime-factors [n]
(if (< n 2)
(loop [x (Math/abs n) d 2 ac ]
(if (== x d)
(conj ac x)
(if (zero? (mod x d))
(recur (/ x d) d (conj ac d))
(recur x (inc d) ac))))))
; determine the frequences of each of the prime-factors
(defn count-factors [n]
(frequencies (prime-factors n)))
; Calculates the smallest positive number that is evenly
; divisible by all of the numbers from 1 to n
(defn smallest-evenly-divisible-to [n]
(let [all-factors (map count-factors (range 1 (inc n)))
comparison #(if (> % %2) % %2)
primes (apply (partial merge-with comparison) all-factors)
pow #(.pow (bigint (key %)) (bigint (val %)))
multiply #(.multiply % (pow %2))]
(reduce multiply (bigint 1) primes)))
Here are some timings for larger and larger numbers.
user=> (time (smallest-evenly-divisible-to 20))
"Elapsed time: 1.029 msecs"
user=> (time (smallest-evenly-divisible-to 200))
"Elapsed time: 12.313 msecs"
user=> (time (smallest-evenly-divisible-to 2000))
"Elapsed time: 198.544 msecs"
15111779487... Too long to post - 868 digits.
user=> (time (smallest-evenly-divisible-to 20000))
"Elapsed time: 13158.299 msecs"
48793256272... Far too large a number to post here, it was 8677 digits long!
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.
So naturally I decided to write a function which accepts a number of digits. To get the answer to problem 4, I would simply send the number 3 to my function:
You can easily solve this problem inefficiently, but I wanted the challenge of making it as efficient as I could! A short and terrible solution would be:
(apply max (filter #(= (str %) (apply str (reverse (str %))))
(for [x (range 100 1000) y (range 100 x)] (* x y))))
This individual’s code works by calculating products for every possible unique combination of two values from 100 to 1000. Then filtering those which are palindromes, and finally taking the largest of those values.
I modified the code to find how long it took to execute for 4-digit values. It took 175646.15 milliseconds, which is approximately 3 minutes.
I’ll talk through how I went about solving it another time, but just know that for 4-digit numbers, mine took on average 9.253 milliseconds. Which is a massive drop from 3 minutes :O.
I came up with the following regular expression to accomplish the job:
It ignores the ‘http://’ segment if it exists, and ignores anything after & including: ‘/’, ‘?’, and ‘#’. ‘/’ indicates a folder, ‘?’ indicates a query, and ‘#’ indicates a fragment. So the following should work:
The Federal Bureau of Investigation moved against a group of suspected online pirates Thursday, targeting the popular file-sharing website megaupload.com a day after Washington lawmakers were besieged by complaints about legislation designed to crack down on the online sharing of pirated copies of music, movies and other material, people familiar with the matter said.
Investigators said there was no connection between arrests in their two-year investigation and the political firestorm that erupted this week over a pending vote on the Stop Online Piracy Act.
This is a big deal, and even if it’s not related to SOPA, the timing certainly does a lot to put it on the minds of those worried about the law.
Omg no I love MegaUpload ;-; It’s the only large-file site other than torrenting that I like.