Problem 4
Ruby
1num = 999;
2current_palin = 0;
3loop do
4 second_num = 999;
5 loop do
6 current_product = num * second_num;
7 if current_product.to_s.reverse == current_product.to_s
8 if current_palin < current_product
9 current_palin = current_product.to_i;
10 end
11 end
12 second_num -= 1;
13 if second_num < 100
14 break;
15 end
16 end
17 num -= 1;
18 if num < 100
19 break;
20 end
21end
22puts"#{current_palin}";
Clojure
1(ns problem4.core)
2
3(defn is-palindrome?
4 [num]
5 (= (str num) (apply str (reverse (str num)))))
6
7(defn get-val
8 [num]
9 (cond
10 (= (is-palindrome? num) true) num
11 :else 0))
12
13(defn cart-prod
14 []
15 (for [x (range 100 1000)
16 y (range 100 1000)]
17 (* x y)))
18
19(defn -main
20 []
21 (println (apply max (map get-val (cart-prod)))))
Perl 6
1say max ([X*] (100..999, 100..999)).grep: {.flip eq $_}