+ 2
algorithm question
im making a simple algorithm and im trying to get a random number from a number what i have now: input: 4,6 forth letter of pi = 1 sixth letter of pi = 9 1 ... 9 = ... but now i want a operator that cannot be reversed like for example + and × can be reversed like 1 + 9 = 10 9 + 1 = 10 1 × 9 = 9 9 × 1 = 9 and i dont want that, does someone know a good operator or formuala?
10 Réponses
+ 9
Nice idea!
The proper term for this is "commutative", when you can swap around the numbers and get the same result.
Some non-commutative functions are
a - b
a / b
a ^ b
a + (b * 10)
a + a + b
...
In fact, only very few functions like `a + b` or `a * b` are commutative and most aren't!
+ 6
Schindlabua Did you have a typo with your last example?
a + a + b
Perhaps you meant:
a * a + b
or
a + a * b
+ 6
Schindlabua Hmmm... thinking...
So... the example above seems to go beyond the standard definition of the cummutative property as I've understood it from basic algebra.
As I understand it, commutative property refers to the order of the operands having no affect on the results.
a + a + b == a + b + a == b + a + a
Based on this understanding, your previous examples above would be written as:
Given a = 0 and b = 1:
0 + 0 + 1 == 0 + 1 + 0 == 1 + 0 + 0 == 1
Likewise, the same is true with different values...
Given a = 1 and b = 0:
1 + 1 + 0 == 1 + 0 + 1 == 0 + 1 + 1 == 2
Maybe my math is a bit too elementary as I did not major in mathematics. 😉
Also, my understanding of associative property is one that involves the regrouping of operands that yield the same results. I can't help but wonder if we're referring to completely different contexts.
I hope this helps clarify my understanding. 😉
+ 6
Schindlabua Ah... I now see the disconnect on my part.
I was really scratching my head because I thought you were speaking of commutative property in the general sense with the latter examples.
Reviewing this now in the context of implementing an operator function, it is now clear that {a and b} refer to input parameters, which are bound to the order in which the argument values (or operands) were passed.
Apologies for dragging that through the mud to reach my understanding. Thanks so much for getting me there. 😉👌
+ 5
Schindlabua How is (a + a + b) non-commutative? That's the expression I was referring to. 🤓
Is it because there are more than 2 terms?
Also, wouldn't the 4th example be commutative:
a + (b *10)
... as long as the parentheses are preserved with the grouping?
I'm just trying to get clarification for others reading this. 😉
Also, maybe my math is just way too rusty, in which case, this will clear things up for me. 🤓
+ 5
David Carroll Ha, I think you may be thinking of associativity!
Let's define a funny operator:
a [^_^] b := a + a + b
(:= means "is defined as")
Now lets test for commutativity:
0 [^_^] 1 == 0 + 0 + 1 == 1
1 [^_^] 0 == 1 + 1 + 0 == 2
Swapping operands does not yield the same result, hence it's not commutative!
+ 4
David Carroll Sorry I had no cellphone connection for a while :D
Yeah those definitions are correct! But nothing is stopping us from defining custom operators, which are just 2-parameter functions.
Granted I was a bit sloppy with notation in the above posts. What I mean exactly is that given a function `f`, it is commutative iff, for all `a` and `b`:
f(a, b) == f(b, a)
So, for example, this function is commutative:
f(a, b) = a + b
How so? We can swap the parameters:
f(b, a) = b + a
And from the commutative property of `+` we know that `a + b == b + a` is correct.
This function is not:
g(a, b) = a + b + b
Since, if we swap the parameters
g(b, a) = b + a + a
We clearly see that `a + b + b ≠ b + a + a`.
Now all we need is some symbol we can put in between the operands, like `a ⭕ b` which we take to mean `g(a, b)` and we are back in operator-land. Our operator `⭕` is non-commutative which is what I meant in my original post :)
+ 2
David Carroll I just typed out random symbols, either is fine :P
(and all of them are non-commutative)
+ 2
Suggestions made so far are good. I would like to add the mod operator, it is non-commutative and it is hard to reverse from the answer and 1 operand to get the other. It is used in most random number generation and encryption implementations for this reason.
+ 1
Well it was a not very obvious assumption to make so thanks for keeping me honest :P