none
Find multiples of a number

    Question

  • Hi,

    I was trying to define a function that would take a number and return a sequence of it's multiples, but it gets stuck somehow. Here it is:

    let Multiplos x : seq<bigint>
        seq { // <- here
            for i in x .. infinito do
                if x % i = 0I then yield i
            }

    I tried to debug it, but the call stack gets stuck in the line marked with a comment. Also, if anything seems strange or stupid in it, I'm willing to change the function itself to an optimized version. "infinito" is suppose to be a representation of positive infinite, but I'm yet to find how to do it (here).

    Thanks for reading,


    João Miguel

    Sunday, November 11, 2012 9:13 PM

Answers

  • Not sure what means 'not working'. When I copy & paste it into FSI it does not complain, and multiplos 10I;; then returns a sequence of 10I 20I 30I ... I'm sure you've copied it incorrectly.

    In your Multiplos you check if x % i = 0I. You should instead check if i % x = 0I, right? The function gets stuck, because no number i greater than x satisfies x % i = 0I, so it searches for the one eternally.

    Also, just a note. I'm glad F# works good in Linux too.
    • Edited by lostmsu Monday, November 12, 2012 8:57 PM
    • Marked as answer by JMCF125 Tuesday, November 13, 2012 4:16 PM
    Monday, November 12, 2012 8:53 PM

All replies

  • I guess you need truly infinite sequence here. For example:

    let unboundedFrom start =
      Seq.unfold (fun c -> Some(c, c + 1I)) start
    let multiplos x =
      unboundedFrom 1I
      |> Seq.map ((*) x)

    Monday, November 12, 2012 12:21 AM
  • Your "multiplos" function isn't working. The types are scrambled, even if I declare the parameter and return type as bigint. Here's the error (FS0001): This expression was expected to have type     seq<System.Numerics.BigInteger> -> 'a     but here has type     seq<'b>

    And can you explain to me why doesn't this work:

    let Multiplos x : seq<bigint> =
        seq {
            for i in infinitoDesde x do // infinitoDesde does the same as unboundedFrom
                if x % i = 0I then yield i
            }

    It compiles but it gets stuck. I'm using MonoDevelop, however, I can use the default Microsoft compiler. I tried with Microsoft's and Mono's compiler, yet when analizing the locals I can see the following:


    What's going on here? Everything seems ok, and it only gets stuck on this exact point. As expected from this behaviour, if this function is inserted in F# Interactive it runs forever.


    João Miguel

    Monday, November 12, 2012 8:31 PM
  • Not sure what means 'not working'. When I copy & paste it into FSI it does not complain, and multiplos 10I;; then returns a sequence of 10I 20I 30I ... I'm sure you've copied it incorrectly.

    In your Multiplos you check if x % i = 0I. You should instead check if i % x = 0I, right? The function gets stuck, because no number i greater than x satisfies x % i = 0I, so it searches for the one eternally.

    Also, just a note. I'm glad F# works good in Linux too.
    • Edited by lostmsu Monday, November 12, 2012 8:57 PM
    • Marked as answer by JMCF125 Tuesday, November 13, 2012 4:16 PM
    Monday, November 12, 2012 8:53 PM
  • Not sure what means 'not working'. When I copy & paste it into FSI it does not complain, and multiplos 10I;; then returns a sequence of 10I 20I 30I ... I'm sure you've copied it incorrectly.

    In your Multiplos you check if x % i = 0I. You should instead check if i % x = 0I, right? The function gets stuck, because no number i greater than x satisfies x % i = 0I, so it searches for the one eternally.

    Also, just a note. I'm glad F# works good in Linux too.
    Ahrg! How couldn't i notice such a big mistake?!? Well, this is what I get for coding in a hurry.

    João Miguel

    Monday, November 12, 2012 10:23 PM
  • Also, just a note. I'm glad F# works good in Linux too.

    Yes, thanks to the Mono project and MonoDevelop, C# and F# (in the F# case, you'll need an extension).

    I'm glad you did find that error (the), but that one was not the reason of why it wasn't working. While debbuging, I noticed the error still appears (as the image in the other replie shows), although in F# Interactive it works. Then, when I click in the equivelent of "step into" quite a lot of times, MonoDevelop crashes.

    Hope someone can help me,


    João Miguel

    Tuesday, November 13, 2012 3:41 PM
  • I found something that if there are no breakpoints, it works perfectly. Quite strange indeed...

    Anyway, @lostmsu, thanks for answering the question by identifying the first error (the swap of x and i).


    João Miguel

    Tuesday, November 13, 2012 4:16 PM