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. The Lounge
  3. Gah! When you're so close to an answer but it is so far away

Gah! When you're so close to an answer but it is so far away

Scheduled Pinned Locked Moved The Lounge
tutorial
19 Posts 8 Posters 6 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.
  • H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #1

    I need to permutate for every combination of 2 colors in a palette. More specifically I need to compute the size of this. It would normally be N*N I think where N is the number of colors in the palette Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one. This is simple enough to find every pair, but I need the count of how many pairs I need based on N. This should be easy. I can *almost* wrap my head around it and that's - it's something like N*N/2 or maybe N*(N-1) I think. But it's weird to check it. I hate problems like this.

    Real programmers use butterflies

    J C OriginalGriffO F 4 Replies Last reply
    0
    • H honey the codewitch

      I need to permutate for every combination of 2 colors in a palette. More specifically I need to compute the size of this. It would normally be N*N I think where N is the number of colors in the palette Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one. This is simple enough to find every pair, but I need the count of how many pairs I need based on N. This should be easy. I can *almost* wrap my head around it and that's - it's something like N*N/2 or maybe N*(N-1) I think. But it's weird to check it. I hate problems like this.

      Real programmers use butterflies

      J Offline
      J Offline
      Jon McKee
      wrote on last edited by
      #2

      honey the codewitch wrote:

      Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one.

      You're looking for combinations not permutations. I really like [this site](https://www.mathsisfun.com/combinatorics/combinations-permutations.html) for basic math stuff. They do a good job of making things approachable without getting too into the weeds. TL;DR: You're looking for something like n!/(r!(n - r)!) where n is the number of things to choose from and r is how many you choose, where there are no repetitions and order doesn't matter.

      H 1 Reply Last reply
      0
      • J Jon McKee

        honey the codewitch wrote:

        Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one.

        You're looking for combinations not permutations. I really like [this site](https://www.mathsisfun.com/combinatorics/combinations-permutations.html) for basic math stuff. They do a good job of making things approachable without getting too into the weeds. TL;DR: You're looking for something like n!/(r!(n - r)!) where n is the number of things to choose from and r is how many you choose, where there are no repetitions and order doesn't matter.

        H Offline
        H Offline
        honey the codewitch
        wrote on last edited by
        #3

        Thanks actually. I just had someone on reddit clear it up for me. I'm crap at math/maths :~

        Real programmers use butterflies

        J N enhzflepE 3 Replies Last reply
        0
        • H honey the codewitch

          Thanks actually. I just had someone on reddit clear it up for me. I'm crap at math/maths :~

          Real programmers use butterflies

          J Offline
          J Offline
          Jon McKee
          wrote on last edited by
          #4

          honey the codewitch wrote:

          I'm crap at math/maths

          Given your knowledge of other areas I have no doubt you could be good if you wanted to :-D Basic combinatorics is pretty straightforward compared to other stuff; the equations just look menacing.

          H 2 Replies Last reply
          0
          • H honey the codewitch

            I need to permutate for every combination of 2 colors in a palette. More specifically I need to compute the size of this. It would normally be N*N I think where N is the number of colors in the palette Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one. This is simple enough to find every pair, but I need the count of how many pairs I need based on N. This should be easy. I can *almost* wrap my head around it and that's - it's something like N*N/2 or maybe N*(N-1) I think. But it's weird to check it. I hate problems like this.

            Real programmers use butterflies

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Quote:

            But it's weird to check it.

            It is simple to check it: you may choose the first color from the N ones, then you may choose the second color from the (N-1) remaining ones. So, I would go for the N(N-1) 'proposal'. :)

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            H 1 Reply Last reply
            0
            • J Jon McKee

              honey the codewitch wrote:

              I'm crap at math/maths

              Given your knowledge of other areas I have no doubt you could be good if you wanted to :-D Basic combinatorics is pretty straightforward compared to other stuff; the equations just look menacing.

              H Offline
              H Offline
              honey the codewitch
              wrote on last edited by
              #6

              Unfortunately, due to the size of the numbers w/ factorials it's not feasible for me to use that solution. I'll have to just allocate memory as I go. Edit: NVM it can be reduced to eliminate the factorials.

              Real programmers use butterflies

              1 Reply Last reply
              0
              • C CPallini

                Quote:

                But it's weird to check it.

                It is simple to check it: you may choose the first color from the N ones, then you may choose the second color from the (N-1) remaining ones. So, I would go for the N(N-1) 'proposal'. :)

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #7

                I wasn't being very clear. I can't use a brute force method of iteration to check it because I actually want to use the resulting formula to check *that* against, if that makes sense.

                Real programmers use butterflies

                C 1 Reply Last reply
                0
                • J Jon McKee

                  honey the codewitch wrote:

                  I'm crap at math/maths

                  Given your knowledge of other areas I have no doubt you could be good if you wanted to :-D Basic combinatorics is pretty straightforward compared to other stuff; the equations just look menacing.

                  H Offline
                  H Offline
                  honey the codewitch
                  wrote on last edited by
                  #8

                  It's kind of funny how my mind works. I'm great at some things, like I can think in several levels of abstraction at once, which helps immensely with coding. And simple arithmetic like pointer math I'm good at, as well as sets (usually though I still get tripped up sometimes) and (related) lambda calculus. However, go to geometry or anything non-trivial in algebra and you lose me. Plus there are gaping holes in my knowledge due to my lack of schooling. I didn't even know trig was based around triangles until my 20s. :laugh:

                  Real programmers use butterflies

                  J 1 Reply Last reply
                  0
                  • H honey the codewitch

                    I need to permutate for every combination of 2 colors in a palette. More specifically I need to compute the size of this. It would normally be N*N I think where N is the number of colors in the palette Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one. This is simple enough to find every pair, but I need the count of how many pairs I need based on N. This should be easy. I can *almost* wrap my head around it and that's - it's something like N*N/2 or maybe N*(N-1) I think. But it's weird to check it. I hate problems like this.

                    Real programmers use butterflies

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    You may find this useful: Permutations, Combinations, and Variations using C# Generics[^] I use his code in a couple of projects, and the explanations are pretty good.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    H 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      You may find this useful: Permutations, Combinations, and Variations using C# Generics[^] I use his code in a couple of projects, and the explanations are pretty good.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      H Offline
                      H Offline
                      honey the codewitch
                      wrote on last edited by
                      #10

                      Ah thanks

                      Real programmers use butterflies

                      1 Reply Last reply
                      0
                      • H honey the codewitch

                        Thanks actually. I just had someone on reddit clear it up for me. I'm crap at math/maths :~

                        Real programmers use butterflies

                        N Offline
                        N Offline
                        NeverJustHere
                        wrote on last edited by
                        #11

                        I can help you with math/maths. math/maths = 1/s

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          I wasn't being very clear. I can't use a brute force method of iteration to check it because I actually want to use the resulting formula to check *that* against, if that makes sense.

                          Real programmers use butterflies

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #12

                          Brute force?! So the argument in favour of N(N-1) does not convince you? [update] It definitely did not convince Caslen :laugh: :laugh: :laugh: [/update]

                          "In testa che avete, Signor di Ceprano?" -- Rigoletto

                          C 1 Reply Last reply
                          0
                          • H honey the codewitch

                            It's kind of funny how my mind works. I'm great at some things, like I can think in several levels of abstraction at once, which helps immensely with coding. And simple arithmetic like pointer math I'm good at, as well as sets (usually though I still get tripped up sometimes) and (related) lambda calculus. However, go to geometry or anything non-trivial in algebra and you lose me. Plus there are gaping holes in my knowledge due to my lack of schooling. I didn't even know trig was based around triangles until my 20s. :laugh:

                            Real programmers use butterflies

                            J Offline
                            J Offline
                            Jon McKee
                            wrote on last edited by
                            #13

                            For me, the trick to making math click was realizing the tools themselves are actually pretty simple. It's just specific examples/problems that make them look complex. Like an integral in its basic form is just the summation of a bunch of rectangular area calculations. So integral_{a}^{b} (2x * 1dx) just means "from a to b, add up all the rectangles with a height of 2x units and a width of 1 unit." The "dx" is just there to let you know what the independent variable is. What you're integrating "with respect to." Line and surface integrals are just taking this same idea, and using geometry to figure out what the "1" should be since we're not doing the integral with respect to the x-axis anymore. Not trying to pressure you into learning more math, I just think schools do a generally poor job at really teaching the basics. I mostly just memorized how to solve problems for a long time because that's what schools teach, but it wasn't until the why of the "how" clicked that I actually started enjoying math.

                            H 1 Reply Last reply
                            0
                            • J Jon McKee

                              For me, the trick to making math click was realizing the tools themselves are actually pretty simple. It's just specific examples/problems that make them look complex. Like an integral in its basic form is just the summation of a bunch of rectangular area calculations. So integral_{a}^{b} (2x * 1dx) just means "from a to b, add up all the rectangles with a height of 2x units and a width of 1 unit." The "dx" is just there to let you know what the independent variable is. What you're integrating "with respect to." Line and surface integrals are just taking this same idea, and using geometry to figure out what the "1" should be since we're not doing the integral with respect to the x-axis anymore. Not trying to pressure you into learning more math, I just think schools do a generally poor job at really teaching the basics. I mostly just memorized how to solve problems for a long time because that's what schools teach, but it wasn't until the why of the "how" clicked that I actually started enjoying math.

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #14

                              I have to learn the "why"s first or I don't retain anything. My associative memory is great, so if I understand how something works I can remember it, but the rest of my memory is pretty terrible. I wish I could learn the other ways too.

                              Real programmers use butterflies

                              1 Reply Last reply
                              0
                              • H honey the codewitch

                                I need to permutate for every combination of 2 colors in a palette. More specifically I need to compute the size of this. It would normally be N*N I think where N is the number of colors in the palette Except I need to subtract instances. For example, when I have pair (A,B) and pair (B,A), I should only accept one. This is simple enough to find every pair, but I need the count of how many pairs I need based on N. This should be easy. I can *almost* wrap my head around it and that's - it's something like N*N/2 or maybe N*(N-1) I think. But it's weird to check it. I hate problems like this.

                                Real programmers use butterflies

                                F Offline
                                F Offline
                                Fueled By Decaff
                                wrote on last edited by
                                #15

                                These will be the triangular numbers: Triangular number - Wikipedia[^] Gives the formula: count = n(n+1)/2

                                1 Reply Last reply
                                0
                                • H honey the codewitch

                                  Thanks actually. I just had someone on reddit clear it up for me. I'm crap at math/maths :~

                                  Real programmers use butterflies

                                  enhzflepE Offline
                                  enhzflepE Offline
                                  enhzflep
                                  wrote on last edited by
                                  #16

                                  One of Neil deGrasse Tyson's quotes comes to mind here. You know? The one that ends with "illiterate dolt".. :-D Obviously doesn't apply to ya, but the reminder has left me laughing and the cat looking at me with a "wtf did you wake me up for?" look. Thanks :thumbsup:

                                  1 Reply Last reply
                                  0
                                  • C CPallini

                                    Brute force?! So the argument in favour of N(N-1) does not convince you? [update] It definitely did not convince Caslen :laugh: :laugh: :laugh: [/update]

                                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                    C Offline
                                    C Offline
                                    Caslen
                                    wrote on last edited by
                                    #17

                                    I could be wrong but... shouldn't it be N(N-1)/2 to get the unique combinations? (ie discarding one of AB or BA combinations as required in the OP)

                                    C 1 Reply Last reply
                                    0
                                    • C Caslen

                                      I could be wrong but... shouldn't it be N(N-1)/2 to get the unique combinations? (ie discarding one of AB or BA combinations as required in the OP)

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #18

                                      In fact, you are right and I was wrong :-O It is

                                      C(N,k) = N! / ((N-k)! k! )

                                      with k=2.

                                      "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                      C 1 Reply Last reply
                                      0
                                      • C CPallini

                                        In fact, you are right and I was wrong :-O It is

                                        C(N,k) = N! / ((N-k)! k! )

                                        with k=2.

                                        "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                        C Offline
                                        C Offline
                                        Caslen
                                        wrote on last edited by
                                        #19

                                        Even the best slip up sometimes :) It also depends on whether you count the AA, BB combinations, I think not in this case, but if so then it would simply be N+N(N-1)/2. Either way your are still correct in that no factorials have to be calculated!

                                        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