F# stream of armstrong numbers -


I am looking for help, mainly because I am very new to the F # environment to generate infinite stream of Armstrong numbers I need to use the F # stream. Anyone can help with this one I have done a few mombo jumbo but I have no clue where I am going.

  type 'a stream = | In spite of 'a * (unit -> a stream), take rec (bracket (x, xsf)) = if n = 0 then [] and x :: le (n -1) (x -f) () ); ; // To test if two integers are equal then test x y = with match (x, y). (X, Y) when X & lt; Y - & gt; Wrong | (X, y) when x & gt; Y - & gt; Wrong | _ - & gt; Let's check to check the correct // armstrong number n = let's move unstable m = n volatile r = 0 let's shaky s = 0 while m & lt; & Gt; 0 do r & lt; - m% 10 s & lt; - s + r * r * rm & lt; - m / 10 If test (ns) then correct and wrong REC Armstrong N = opposition (n, funny () -> if check (N + 1) then Armstrong (N + 1) and Armstrong (N + 2)) Paus = armstrong 0 take 5 paus  

Honestly your code is a bit like a mess looks like.

The most basic version I could think of is:

  Let's say Armstrong (A, B, C) = A * A * A + B * B * B + C * C * C = (A * 100 + B * 10 + C) in armstrong = Seek [0..9] in B [0..9] to do in [0..9] If ararmstrong (A, B, C) is generated (one * 100 + B * 10 + C)}  

Definitely a armstrong number 3-digit number where is the number of cubes of digits The Minister's

This will yield you:

  & gt; Seq.toList Armstrong ;; This value is: int list = [0; 1; 153; 370; 371; 407]  

But it should be easy to add a wide range or remove one digit numbers (think about it).

General case

The problem seems so interesting that I choose to apply the general case (see here):

  two numbers To give = ROR = n = if n = 0 then for [(0, []]] and [for [0..9] for x (n, 1) make (n-1) N, X :: XS]] make Seq.initInfinite | & Gt; Number Seq.concat (DS: Ent List) = DS | & Gt; List.fold (fun s -> * 10i + bigint d) 0 I Armstrong (m: int, ds: at list) = ds. & Gt; List.map (funny D -> Bilent D ** m). & Gt; List.sum leading zero = function | 0 :: _ - & gt; True | _ - & gt; False IA Armstrong (M: int, DS: at list) = If the leading SADSS DS then false and leave = Armstrong (M, DS) right = toNumber ds left = right armstrongs = numbers | & Gt; Seq.filter isArmstrong | & Gt; Seq.map (snd> & gt; toNumber)  

But the numbers get very quickly and it will soon get you out-of-memory, but the first 20 are:

& gt; Seq.take 20 Armstrong | & Gt; Seq.map string | & Gt; Seq.toList ;; This value is: string list = ["0"; "1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"; "9"; "153"; "370"; "371"; "407"; "1634"; "8208"; "9474"; "54,748"; "92727"; "93084"]

comment / disclaimer

This is the most basic version - if you calculate all the digits and use basic mathematics, Get performance / exposure points and exponate;) ... make sure that you can understand it


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -