C
CBFalconer
Ben said:Are there many card games that call for a deck one card short?
When one player keeps an Ace up his sleeve, yes.
Ben said:Are there many card games that call for a deck one card short?
For the CLC readers:
Can you, by artifical construction or actual experience, come up with
something that's more Wrong and yet still manages to be correct code
that performs the intended task?
Chris Croughton said:With a bit more reasonableness, since 13*7 is harder to recognise as
non-prime than 3*17...
Sure, but there's a trick for multiples of 7 as well. Take the last
digit, double it, and subtract from what's left. Iterate as needed.
If the final result is 0 or 7, it's a multiple of 7; if not, not.
9 - (2*1) == 7
(Unlike the multiples-of-3 and multiples-of-9 tricks, it doesn't tell
you the remainder for non-multiples.)
Keith said:.... snip ...
Sure, but there's a trick for multiples of 7 as well. Take the last
digit, double it, and subtract from what's left. Iterate as needed.
If the final result is 0 or 7, it's a multiple of 7; if not, not.
9 - (2*1) == 7
(Unlike the multiples-of-3 and multiples-of-9 tricks, it doesn't tell
you the remainder for non-multiples.)
John said:I once had a do loop:
do {
/* stuff */
} while (condition);
that I later decided needed to be a for loop. So I just edited
the top of the loop, forgetting about the bottom:
for (i = 42; condition; i++) {
/* stuff */
} while (condition);
When I later noticed what I had done, but that the code was still
working correctly, I first thought that the compiler had not
noticed a syntax error.
Keith said:Chris Croughton <[email protected]> writes:
Sure, but there's a trick for multiples of 7 as well. Take the last
digit, double it, and subtract from what's left. Iterate as needed.
If the final result is 0 or 7, it's a multiple of 7; if not, not.
9 - (2*1) == 7
(Unlike the multiples-of-3 and multiples-of-9 tricks, it doesn't tell
you the remainder for non-multiples.)
CBFalconer said:How does that work when you run into negative values. Ex: 427
42 - 2*7 = 28
2 - 2*8 = ???
and why?
How does that work when you run into negative values. Ex: 427
42 - 2*7 = 28
2 - 2*8 = ???
and why?
That's what puzzles me. It does work, it seems, but I don't understand
it...
2 - 2*8 = -14, so ignore the sign and continue:
1 - 2*4 = -7, ignore the sign and it's a multiple of 7.
That's what puzzles me. It does work, it seems, but I don't understand
it...
On Sat, 19 Mar 2005 14:51:43 +0000, Chris Croughton
It's a bit of modular arithmetic. Suppose our number is 10*a + b.
Then
10a+b %7 = 3a+b %7 = 3a-6b %7 = 3(a-2b)%7
If 10a+b=0%7 then 3(a-2b)=0%7, and dividing by 3, (a-2b)=0%7;
more simply 10a+b=0%7 => 10a-20b%7 => a-2b=0%7
Thanks, Richard - that's a lot simpler than my version (though you
accidentally left out the "=0" in the second step). Still, I'm happy
to have puzzled it out on my own.
If anyone doesn't understand the second step, note that -20 is
congruent to 1 mod 7. (-20 plus 21 = 1.) That means multiplying by
-20 is the same as multiplying by 1, as far as congruence modulo 7
goes, so we can freely multiply b by -20. And we choose -20 because
it's the closest multiple of 10 that's 1 mod 7, so that we can divide
out the common factor 10 and simplify the LHS of the equation.
(Clearly I should have started with the modular algebra and not with
drawing up tables of congruences, then working backward from those...)
I understood yours!
Ah, thanks, I wondered where the 20 came from. I'm not happy about
factoring out the 10 in the last step, though, is that valid in modulo
arithmetic? 14 % 7 == 0, but 1.4 % 7 == 1.4, no?
Doing it multiple ways confirms the correctness...
Chris C
Keith said:Sure, but there's a trick for multiples of 7 as well. Take the last
digit, double it, and subtract from what's left. Iterate as needed.
If the final result is 0 or 7, it's a multiple of 7; if not, not.
9 - (2*1) == 7
Rob Thorpe said:It's difficult looking at pieces of code like that you posted.
Sometimes pieces work, but are bad style, others work but are
undefined behaviour, still others are bugs but just happen to work.
When code is contorted it's very hard to tell which is which.
John Temples said:for (i = 42; condition; i++) {
/* stuff */
} while (condition);
When I later noticed what I had done, but that the code was still
working correctly, I first thought that the compiler had not noticed a
syntax error.
Isn't it easier to just divide by 7 ??
Good question. In this instance, yes. 10 and 7 are relatively prime,
so 10 has an inverse modulo 7. (The inverse is 5) We can multiply
both sides by the inverse, which is equivalent to dividing both sides
by 10.
For an example where you can't cancel, take
2x = 0 %6
Cancelling gives us x=0, but x=3 is also a valid solution.
1.4 isn't an integer. The rules for modular arithmetic don't all
apply to non integers.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.