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...
)
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Project Euler 26.
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password