Can you write a function that counts the number of zero digits in an integer? Assuming so, call it "numZeroes".

Then

1
range |> Array.maxBy (fun n -> numZeroes n, n)

would compare the values first by number-of-zeroes, then by value, and yield the one you seek. This is one way to do it.

By on 2/25/2011 5:01 PM ()

Hello, Brian:
Thank you very much for your idea. Your idea seems to be a good one, however, for me, it is not so easy to write a function to count the number of zeros in a number in a functional way, I think I can do this using an imperative way. I like to see if you can offer some code doing this using a functional way.
Thanks,

By on 2/25/2011 9:59 PM ()
1
2
 
let numZeroes i = i |> string |> Seq.filter ((=) '0') |> Seq.length

Or, alternatively (probably a bit faster):

1
2
3
 
open System.Linq
let numZeroes i = string(i).Count( fun c -> c = '0' )
By on 2/27/2011 2:15 PM ()

Hi,
Sorry, I can not get hubFS for a few days, I have issue with internet connection.
Thank you for your answer, but I don't know how to use it:

1
2
3
4
5
6
7
8
9
> open System.Linq
let numZeroes i = string(i).Count( fun c -> c = '0' )
let a10000 = numZeroes 999888
;;

val numZeroes : int -> int
val a10000 : int = 0

>

I care about the 0s, I need the most 0s. So for 999888, I need 900000, not 990000.
But please let me know how I can use your function first.
Thanks,

By on 3/2/2011 9:40 AM ()

Is number of zero's or value more important?

for 1..198765

100000 or 190000 or 198000 or 198700 etc..

perhaps something like

largestnum = max - (max % 100000)

By on 2/25/2011 11:16 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper