F# stream of armstrong numbers -



F# stream of armstrong numbers -

i seeking help, because new f# environment. need utilize f# stream generate infinite stream of armstrong numbers. can 1 help one. have done mambo jumbo have no clue i'm going.

type 'a stream = | cons of 'a * (unit -> 'a stream) allow rec take n (cons(x, xsf)) = if n = 0 [] else x :: take (n-1) (xsf());; //to test if 2 integers equal allow test x y = match (x,y) | (x,y) when x < y -> false | (x,y) when x > y -> false | _ -> true //to check armstrong number allow check n = allow mutable m = n allow mutable r = 0 allow mutable s = 0 while m <> 0 r <- m%10 s <- s+r*r*r m <- m/10 if (test n s) true else false allow rec armstrong n = cons (n, fun () -> if check (n+1) armstrong (n+1) else armstrong (n+2)) allow pos = armstrong 0 take 5 pos

to honest code seems bit mess.

the basic version think of this:

let isarmstrong (a,b,c) = a*a*a + b*b*b + c*c*c = (a*100+b*10+c) allow armstrongs = seq { in [0..9] b in [0..9] c in [0..9] if isarmstrong (a,b,c) yield (a*100+b*10+c) }

of course of study assuming armstrong number 3-digit number sum of cubes of digits number itself

this yield you:

> seq.tolist armstrongs;; val : int list = [0; 1; 153; 370; 371; 407]

but should easy add together wider range or remove one-digit numbers (think it).

general case

the problem seems interesting take implement general case (see here) too:

let numbers = allow rec create n = if n = 0 [(0,[])] else [ x in [0..9] (_,xs) in create (n-1) yield (n, x::xs) ] seq.initinfinite create |> seq.concat allow tonumber (ds : int list) = ds |> list.fold (fun s d -> s*10i + bigint d) 0i allow armstrong (m : int, ds : int list) = ds |> list.map (fun d -> bigint d ** m) |> list.sum allow leadingzero = function | 0::_ -> true | _ -> false allow isarmstrong (m : int, ds : int list) = if leadingzero ds false else allow left = armstrong (m, ds) allow right = tonumber ds left = right allow armstrongs = numbers |> seq.filter isarmstrong |> seq.map (snd >> tonumber)

but numbers sparse , using out-of-memory first 20 are:

> seq.take 20 armstrongs |> seq.map string |> seq.tolist;; val : string list = ["0"; "1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"; "9"; "153"; "370"; "371"; "407"; "1634"; "8208"; "9474"; "54748"; "92727"; "93084"] remark/disclaimer

this basic version - can big speed/performance if enumerate numbers , utilize basic math , exponentiate digits ;) ... sure can figure out

f#

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -