-3

i have constants that represents a prime number. for example,

  enum Signals : long
    {
        LONGPULLBACK = 2,
        SHORTPULLBACK = 3,
        RSIOVERSOLD = 5,
        RSIOVERBOUGHT = 7,
        BOUGHTSIGNAL = 9,
        DOUBLEBOUGHTSIGNAL = 11,
        SOLDSIGNAL = 13,
        DOUBLESOLDSIGNAL = 17,
        MATRENDINGLONG = 19,
        MATRADENINGSHORT = 23


    }

for every signal triggered, i just add it to the sum. there will always be only 1 signal of each type triggered. when i have a sum of 25, is there a function to return the prime number parts of the sum ie 2,23?

junkone
  • 1,427
  • 1
  • 20
  • 45
  • You should use a [Flags Enum](https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=net-5.0) for this, not prime numbers. – Robert Harvey Nov 15 '20 at 19:40
  • 5
    The decomposition of a number in prime factors (multiplication) is unique, but not the decomposition into prime summands. E.g. 20 may be 17+3 or 13+7. For a sum, use numbers of the form 2 ^ n. (Also: 9 is not prime...) – Klaus Gütter Nov 15 '20 at 19:45
  • 1
    There cannot be, because for example 5+7=3+9, 2+3=5, etc – Evk Nov 15 '20 at 19:47
  • 3
    This question makes no sense. First, sums aren't prime-factorable in the first place (see previous comment). But also, _your enum values aren't even all primes_! `BOUGHTSIGNAL = 9`. Why are you using primes here instead of powers of two (i.e. `[Flags]` enum)? – Peter Duniho Nov 15 '20 at 20:08
  • What is the expected result for 24? – mjwills Nov 15 '20 at 20:47
  • 1
    I've updated my answer to include a comment about your original attempt, and how that could be made to work. – ispiro Nov 15 '20 at 21:10

1 Answers1

1

No.

But you can achieve what you want by using flags. Just don't use 0 as one of your flags for one of the values in your list. 0 can only denote None because you'll never be able to check whether a 0 was added. And the values must be powers of 2. So: 1, 2, 4, 8, ... And only up to 63 values (besides the 0) if you're using long to account for the sign bit. I think you can get 64 if you're using ulong.

Example:

[Flags]
enum Signals : long
{
   None = 0,
   LONGPULLBACK = 1,
   SHORTPULLBACK = 2,
   RSIOVERSOLD = 4,
   RSIOVERBOUGHT = 8,
   //etc.
};

(I'll add that what you were trying to do would work if you were multiplying the numbers. But that would make the number pretty big. Also, decomposition wouldn't be as simple.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • I suspect you didn't mean `LONGPULLBACK` to be `0`. – mjwills Nov 15 '20 at 22:14
  • nope. now fixed it as follows [Flags] enum Signals : long { NONE=0, LONGPULLBACK = 2, SHORTPULLBACK = 4, RSIOVERSOLD = 8, RSIOVERBOUGHT = 16, BOUGHTSIGNAL = 32, DOUBLEBOUGHTSIGNAL = 64, SOLDSIGNAL = 128, DOUBLESOLDSIGNAL = 256, MATRENDINGLONG = 512, MATRADENINGSHORT = 1024 }; – junkone Nov 15 '20 at 22:40
  • @junkone You can also write 1 << 0, 1 << 1, 1 << 2, and so on, so you don’t need to calculate the numbers yourself. Also I think it makes it much clearer which enum member is expressed by which bit. – ckuri Nov 15 '20 at 23:05
  • @mjwills Thanks for the correction. Edited now. – ispiro Nov 16 '20 at 19:24