Coffeehouse Thread

34 posts

Project Euler

Back to Forum: Coffeehouse
  • User profile image
    Charles

    Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

    The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.

    Very cool. Anybody here registered? Some really good mind benders.

    Code on,
    C

  • User profile image
    Cannot​Resolve​Symbol

    I registered a while back and finished a couple of them (okay, just the first two...).  I'd like to go back and finish some more sometime if I get the chance (I'm learning Python as a part of an intro to AI course right now...  seems like Python would be well suited for many of these, so maybe that'll give me some motivation to go back and do some more).

  • User profile image
    Charles

    CannotResolveSymbol said:
    I registered a while back and finished a couple of them (okay, just the first two...).  I'd like to go back and finish some more sometime if I get the chance (I'm learning Python as a part of an intro to AI course right now...  seems like Python would be well suited for many of these, so maybe that'll give me some motivation to go back and do some more).
    Excellent.
    C

  • User profile image
    tfraser

    I'm not registered but saw this a couple of years ago and have done a few. Brute-force ftw.

  • User profile image
    obrienslalom

    I am registered.  I used it as a crash course in haskell, seemed like the right type of problems to try out.  It quickly reminded me how many rules of mathematics I have forgotten/never knew.  They are fun though.

  • User profile image
    ZippyV

    Charles said:
    CannotResolveSymbol said:
    *snip*
    Excellent.
    C
    Would F# be the ideal language to solve these problems in .net?

  • User profile image
    Charles

    ZippyV said:
    Charles said:
    *snip*
    Would F# be the ideal language to solve these problems in .net?
    For .NET, F# would work well, I'd imagine. Have you been programming in F#, Zippy?
    C

  • User profile image
    joechung

    ZippyV said:
    Charles said:
    *snip*
    Would F# be the ideal language to solve these problems in .net?
    The four Euler problems I tried last night were easy to do with C# and LINQ.  They were basically one-line queries of the relevant sequences (prime numbers, factors, and Fibonacci) generated by helper generators.

  • User profile image
    evildictait​or

    joechung said:
    ZippyV said:
    *snip*
    The four Euler problems I tried last night were easy to do with C# and LINQ.  They were basically one-line queries of the relevant sequences (prime numbers, factors, and Fibonacci) generated by helper generators.
    For an extra challenge, do them in one line _without_ helper generators. They are all possible using LINQ, Haskell and F# to do in one line with no other code**.

    ** Edit: OK, you may need to define the infinite list of natural numbers as an IEnumerable<int>:

    class NaturalNumbers :IEnumerable<int>, IEnumerator<int> {
      private int val;
      public void Dispose(){}
      public IEnumerator<int> GetEnumerator() { return new NaturalNumbers(); }
      public IEnumerator GetEnumerator() { return new NaturalNumbers(); }
      public int Current { get {return val;} }
      public bool MoveNext(){ val++; return true; }
      public void Reset(){ val = 0; }
      public object Current { get {return val;} }
    }

  • User profile image
    Sven Groot

    evildictaitor said:
    joechung said:
    *snip*
    For an extra challenge, do them in one line _without_ helper generators. They are all possible using LINQ, Haskell and F# to do in one line with no other code**.

    ** Edit: OK, you may need to define the infinite list of natural numbers as an IEnumerable<int>:

    class NaturalNumbers :IEnumerable<int>, IEnumerator<int> {
      private int val;
      public void Dispose(){}
      public IEnumerator<int> GetEnumerator() { return new NaturalNumbers(); }
      public IEnumerator GetEnumerator() { return new NaturalNumbers(); }
      public int Current { get {return val;} }
      public bool MoveNext(){ val++; return true; }
      public void Reset(){ val = 0; }
      public object Current { get {return val;} }
    }

    It's possible to do that with considerably less code too:

    static IEnumerable<int> GetNaturalNumbers()
    {
        int val = 0;
        while( true )
            yield return val++;
    }

  • User profile image
    evildictait​or

    Sven Groot said:
    evildictaitor said:
    *snip*

    It's possible to do that with considerably less code too:

    static IEnumerable<int> GetNaturalNumbers()
    {
        int val = 0;
        while( true )
            yield return val++;
    }

    true - but the yield keyword is weird and does crazy stuff with storing state, so I don't ever use it.

    In either case they do the same thing - yield is syntactic sugar for an anonymous enumerator and enumerable object.

  • User profile image
    stevo_

    evildictaitor said:
    Sven Groot said:
    *snip*
    true - but the yield keyword is weird and does crazy stuff with storing state, so I don't ever use it.

    In either case they do the same thing - yield is syntactic sugar for an anonymous enumerator and enumerable object.
    Crazy stuff? also why would I write an enumerator and enumerable wrapper manually when I can do svens version which basically gets the compiler to do all that for me? its not vodoo.. plus I think theres a bug with yours because your enumerable returns the same enumerator (itself).. so you cannot get two unique enumerators.. ie, technically I can get the enumerable of svens and GetEnumerator multiple times and get a unique enumerator each time (with its own unique value state).. where as yours I'd need to create a new enumerable each time..

  • User profile image
    ZippyV

    Charles said:
    ZippyV said:
    *snip*
    For .NET, F# would work well, I'd imagine. Have you been programming in F#, Zippy?
    C
    We could create a wiki on C9 that contain all the working solutions in different .net languages: C#, VB, maybe F#.

    I thought F# was the ideal language for solving mathematical things or is this totally wrong?

  • User profile image
    Sven Groot

    evildictaitor said:
    Sven Groot said:
    *snip*
    true - but the yield keyword is weird and does crazy stuff with storing state, so I don't ever use it.

    In either case they do the same thing - yield is syntactic sugar for an anonymous enumerator and enumerable object.
    true - but the yield keyword is weird and does crazy stuff with storing state, so I don't ever use it.

    What it does is create a class that's pretty near identical to what you wrote above. I see no reason not to use it.

  • User profile image
    evildictait​or

    Sven Groot said:
    evildictaitor said:
    *snip*

    What it does is create a class that's pretty near identical to what you wrote above. I see no reason not to use it.
    Sure whatever. Use yield if you want to, but I know how it works, and it's a bit yucky so I refuse to use it on principle (it's basically a finite state machine with lots of locals and some nasty tricks), so I just tend to write it manually.

    @stevo: I just wrote it off the top of my head. Use new NaturalNumbers() in GetEnumerator() if you care, I wasn't expecting the entire thread to derail while everone critiques it.

  • User profile image
    Sven Groot

    evildictaitor said:
    Sven Groot said:
    *snip*
    Sure whatever. Use yield if you want to, but I know how it works, and it's a bit yucky so I refuse to use it on principle (it's basically a finite state machine with lots of locals and some nasty tricks), so I just tend to write it manually.

    @stevo: I just wrote it off the top of my head. Use new NaturalNumbers() in GetEnumerator() if you care, I wasn't expecting the entire thread to derail while everone critiques it.
    Sure whatever. Use yield if you want to, but I know how it works, and it's a bit yucky so I refuse to use it on principle (it's basically a finite state machine with lots of locals and some nasty tricks), so I just tend to write it manually.

    I know how it works too, but I value my own time more than how clean some generated code that hardly anyone will ever see anyway looks.

    I'm not attacking you for not liking yield or anything, I simply wanted to point out an alternative. I didn't expect the thread to completely derail either.

  • User profile image
    RamblingGeek​UK

    Smiley I know the guy behind this.   Smart guy.

  • User profile image
    Charles

    ZippyV said:
    Charles said:
    *snip*
    We could create a wiki on C9 that contain all the working solutions in different .net languages: C#, VB, maybe F#.

    I thought F# was the ideal language for solving mathematical things or is this totally wrong?
    Functional languages are very effective and efficient for mathematical programming (some more than others). F# provides a functional foundation in an envirnoment you're most likely used to (Visual Studio and .NET). So, it is a great choice for these problems. And, yes, please do create a wiki! If you do, then I'll make sure the likes of Erik Meijer and Brian Beckman visit it and add ther own implementations Smiley

    C

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.