Steve Graham
3/22/2016 6:33:00 PM
What does "both truncatable from left to right and right to left" mean?
On Monday, March 21, 2016 at 9:02:23 PM UTC-7, WJ wrote:
> Find the sum of the only eleven primes that are both
> truncatable from left to right and right to left.
>
> OCaml:
>
> let isqrt n = int_of_float (sqrt (float n)) ;;
>
> let is_prime = function
> 0 | 1 -> false
> | 2 -> true
> | n when 0 = n land 1 -> false
> | n ->
> let top = isqrt n in
> let rec loop i =
> if i > top then
> true
> else
> if 0 = (n mod i) then
> false
> else loop (i+2)
> in loop 3;;
>
> let is_prime' = function
> 0 | 1 -> false
> | 2 | 3 -> true
> | n when (0 = n land 1) || (0=n mod 3) -> false
> | n ->
> let top = isqrt n in
> let rec loop i count =
> if i > top then
> true
> else
> if 0 = (n mod i) then
> false
> else loop (i+2+2*(count land 1)) (count+1)
> in loop 5 0;;
>
> let rec right_truncatable n =
> if 0 = n then true
> else if is_prime n then
> right_truncatable (n / 10)
> else false ;;
>
> let rec digits_of_int' accum = function
> 0 -> if accum = [] then [0] else accum
> | n -> digits_of_int' (n mod 10::accum) (n / 10) ;;
> let digits_of_int = digits_of_int' [] ;;
>
> let int_from_digits list =
> List.fold_left (fun acc n -> acc*10 + n) 0 list ;;
>
> let rec left_truncatable n =
> let rec loop = function
> [] -> true
> | xs -> if is_prime (int_from_digits xs)
> then loop (List.tl xs) else false
> in loop (List.tl (digits_of_int n)) ;;
>
> let truncatable n = (right_truncatable n) && (left_truncatable n) ;;
>
> let check n =
> if (is_prime n) && (truncatable n)
> then [n; 1]
> else [0; 0] ;;
>
> let rec find n = function
> [sum; 11] -> sum
> | status ->
> find (n+2) (List.map2 (+) (check n) status) ;;
>
> find 9 [0;0];;
>
> ===>
> 748317
>
>
> Let's see a Lispy solution.
>
> --
> [T]he owners of the Federal Reserve are ... 1. Rothschild Bank ... 2. Lazard
> Brothers Banks ... 3. Israel Moses Saif Banks ... 4. Warburg Bank ... 5. Lehman
> Brothers Bank ... 6. Kuhn, Loeb Bank ... 7. Chase Manhattan Bank ... 8.
> Goldman, Sachs .... All the owners are obviously [Jews], with the possible
> exception of the Rockefellers.... --- R. P. Oliver