Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. no iterations

no iterations

Scheduled Pinned Locked Moved Clever Code
questionphpcombeta-testing
22 Posts 9 Posters 82 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Andrew Rissing

    public static string[] RubeGoldberg(int n)
    {
    ...
    }

    Named after, ofcourse, Rube Goldberg[^].

    A Offline
    A Offline
    Andrew Rissing
    wrote on last edited by
    #7

    Btw, I hope the OP does realize that you haven't infact removed the loop, but just hidden it inside a method call. I can just see the teacher looking at this and going...you know...I really just wanted a recursive method.

    L 1 Reply Last reply
    0
    • A Andrew Rissing

      Btw, I hope the OP does realize that you haven't infact removed the loop, but just hidden it inside a method call. I can just see the teacher looking at this and going...you know...I really just wanted a recursive method.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #8

      well, a recursion is also a loop, as it executes the same code over and over again; luckily all he really asked to get rid of was a for loop, so a simple while could have sufficed. PS: yeah, I don't mind freaking out a teacher once in a while. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

      A 1 Reply Last reply
      0
      • A Andrew Rissing

        public static string[] RubeGoldberg(int n)
        {
        ...
        }

        Named after, ofcourse, Rube Goldberg[^].

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #9

        hmm. Not quite, Binet's formula would give better performance than any loop or recursion once n goes up. And please don't think of doing these things recursively (unless you cache intermediate results), it would take very long as it has exponential behavior. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

        A 1 Reply Last reply
        0
        • L Luc Pattyn

          well, a recursion is also a loop, as it executes the same code over and over again; luckily all he really asked to get rid of was a for loop, so a simple while could have sufficed. PS: yeah, I don't mind freaking out a teacher once in a while. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

          A Offline
          A Offline
          Andrew Rissing
          wrote on last edited by
          #10

          To me, it just sounded like there was a bit of confusion on his part as to what he was really aiming for. Hence, my observation.

          1 Reply Last reply
          0
          • L Luc Pattyn

            hmm. Not quite, Binet's formula would give better performance than any loop or recursion once n goes up. And please don't think of doing these things recursively (unless you cache intermediate results), it would take very long as it has exponential behavior. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

            A Offline
            A Offline
            Andrew Rissing
            wrote on last edited by
            #11

            NetQuestions wrote:

            I want to initialize a string array with "*".

            That part and the subsequent responses regarding that from you and Ennis just confused me over what he actually wants. </spam>

            1 Reply Last reply
            0
            • L Luc Pattyn

              Can you come up with an appropriate name for this method:

              public static string[] xyz(int n) {
              if (n<1) throw new ArgumentException("n must be greater than zero");
              return new string('*', (int)Math.Round((Math.Pow(0.5+Math.Sqrt(1.25), n)-Math.Pow(0.5-Math.Sqrt(1.25), n))/Math.Sqrt(5))).Replace("*", ".*").Substring(1).Split('.');
              }

              PS: triggered by an actual programming question (which looked like homework). :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #12

              MyGodItsFullOfStars

              L A 2 Replies Last reply
              0
              • P PIEBALDconsult

                MyGodItsFullOfStars

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #13

                BestSoFar :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Sorry, rejected by at least a dozen FxCop rules. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                  A Offline
                  A Offline
                  Abhinav S
                  wrote on last edited by
                  #14

                  Luc Pattyn wrote:

                  Sorry, rejected by at least a dozen FxCop rules.

                  GAH - you did not tell me you had FXCop running. :)

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    MyGodItsFullOfStars

                    A Offline
                    A Offline
                    Andrew Rissing
                    wrote on last edited by
                    #15

                    :laugh: Got my vote.

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Can you come up with an appropriate name for this method:

                      public static string[] xyz(int n) {
                      if (n<1) throw new ArgumentException("n must be greater than zero");
                      return new string('*', (int)Math.Round((Math.Pow(0.5+Math.Sqrt(1.25), n)-Math.Pow(0.5-Math.Sqrt(1.25), n))/Math.Sqrt(5))).Replace("*", ".*").Substring(1).Split('.');
                      }

                      PS: triggered by an actual programming question (which looked like homework). :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                      R Offline
                      R Offline
                      ricmil42
                      wrote on last edited by
                      #16

                      FibonacciStrings ( )

                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Can you come up with an appropriate name for this method:

                        public static string[] xyz(int n) {
                        if (n<1) throw new ArgumentException("n must be greater than zero");
                        return new string('*', (int)Math.Round((Math.Pow(0.5+Math.Sqrt(1.25), n)-Math.Pow(0.5-Math.Sqrt(1.25), n))/Math.Sqrt(5))).Replace("*", ".*").Substring(1).Split('.');
                        }

                        PS: triggered by an actual programming question (which looked like homework). :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                        D Offline
                        D Offline
                        dawmail333
                        wrote on last edited by
                        #17

                        HereBeDragons(n) ;P Or even better: uβerπ(n) :laugh: EDIT: If you can't read that, it says uBer(pi)

                        1 Reply Last reply
                        0
                        • R ricmil42

                          FibonacciStrings ( )

                          A Offline
                          A Offline
                          Abhinav S
                          wrote on last edited by
                          #18

                          ricmil42 wrote:

                          FibonacciStrings ( )

                          That sounds like an actual answer - no fun. ;P

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Can you come up with an appropriate name for this method:

                            public static string[] xyz(int n) {
                            if (n<1) throw new ArgumentException("n must be greater than zero");
                            return new string('*', (int)Math.Round((Math.Pow(0.5+Math.Sqrt(1.25), n)-Math.Pow(0.5-Math.Sqrt(1.25), n))/Math.Sqrt(5))).Replace("*", ".*").Substring(1).Split('.');
                            }

                            PS: triggered by an actual programming question (which looked like homework). :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                            Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                            H Offline
                            H Offline
                            Hired Mind
                            wrote on last edited by
                            #19

                            How about:

                                private static readonly double WhatDoesThisMean = 0.5 + Math.Sqrt(5);
                                private static readonly double WhatDoesThisMean2 = 0.5 - Math.Sqrt(1.25);
                            
                                public static string\[\] **DesparatelyNeedsRefactoring**(int n)
                                {
                                    if (n < 1) 
                                        throw new ArgumentException("n must be greater than zero");
                            
                                    double d = Math.Pow(WhatDoesThisMean, n) - Math.Pow(WhatDoesThisMean2, n);
                            
                                    var stringLength = (int) Math.Round(d / WhatDoesThisMean);
                                    return new string('\*', stringLength).Replace("\*", ".\*").Substring(1).Split('.');
                                }
                            

                            Note that I would refactor the variable 'stringLength' out as well - I put that in there to try and figure out what was going on, before I gave up and Googled.

                            Before .NET 4.0, object Universe = NULL;

                            L 1 Reply Last reply
                            0
                            • H Hired Mind

                              How about:

                                  private static readonly double WhatDoesThisMean = 0.5 + Math.Sqrt(5);
                                  private static readonly double WhatDoesThisMean2 = 0.5 - Math.Sqrt(1.25);
                              
                                  public static string\[\] **DesparatelyNeedsRefactoring**(int n)
                                  {
                                      if (n < 1) 
                                          throw new ArgumentException("n must be greater than zero");
                              
                                      double d = Math.Pow(WhatDoesThisMean, n) - Math.Pow(WhatDoesThisMean2, n);
                              
                                      var stringLength = (int) Math.Round(d / WhatDoesThisMean);
                                      return new string('\*', stringLength).Replace("\*", ".\*").Substring(1).Split('.');
                                  }
                              

                              Note that I would refactor the variable 'stringLength' out as well - I put that in there to try and figure out what was going on, before I gave up and Googled.

                              Before .NET 4.0, object Universe = NULL;

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #20

                              Hired Mind wrote:

                              I gave up and Googled

                              and? :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                              H 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hired Mind wrote:

                                I gave up and Googled

                                and? :)

                                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                H Offline
                                H Offline
                                Hired Mind
                                wrote on last edited by
                                #21

                                Got distracted by productive work, and then you gave a the answer in another post. Fibonacci sequence right?

                                Before .NET 4.0, object Universe = NULL;

                                H 1 Reply Last reply
                                0
                                • H Hired Mind

                                  Got distracted by productive work, and then you gave a the answer in another post. Fibonacci sequence right?

                                  Before .NET 4.0, object Universe = NULL;

                                  H Offline
                                  H Offline
                                  Hal Angseesing
                                  wrote on last edited by
                                  #22

                                  I must say I was rather pleased with myself at having worked out the correct answer before reading this post (and another) with the correct answer in. Of course if anybody actually implemented this in production code they would be told off. Reminds me of the pointer trickery fun we used to do in C "for performance reasons" :)

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups