[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Project Euler 26.

William James

3/18/2016 7:15:00 AM

Find the value of d < 1000 for which 1/d contains the longest
recurring cycle in its decimal fraction part.


OCaml:

#load "nums.cma";;
open Num;;
#load "str.cma";;

let rec reduce s =
if Str.string_match (Str.regexp "\\(.+\\)\\1+$") s 0
then reduce (Str.matched_group 1 s)
else s ;;

let examine d =
let s = approx_num_fix 4_000 (Int 1 // Int d) in
if
Str.string_match (Str.regexp ".*\\(......+\\)\\1\\1") s 0
then
let cycled = reduce (Str.matched_group 1 s) in
(String.length cycled, d, cycled)
else (0,0,"") ;;

let best = ref (0,0,"") ;;
for d = 3 to 999 do
best := max !best (examine d)
done ;;
let (len,n,_) = !best in
Printf.printf "%d yields longest cycle (length %d)\n" n len;;

983 yields longest cycle (length 982)


--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K...)