Calcudoku puzzle forum
https://www.calcudoku.org/forum/

mod and negative numbers
https://www.calcudoku.org/forum/viewtopic.php?f=3&t=1089
Page 1 of 2

Author:  paulv66  [ Wed Aug 08, 2018 8:03 pm ]
Post subject:  mod and negative numbers

I have a question about the mod function where negative numbers are included. Two of the puzzles in the new book have numbers from -1 to +7 and include some cells with the mod function. I'm trying to figure out the logic where one of these cells includes -1.

if we look at a 'normal' cell with positive only values - say 2,5 - the answer can be either 1 or 2. If zero is included, the only answer can be 0 as division by 0 is undefined. If we take a cell with -1 and 4 (for example), -1 divided by 4 leaves a remainder of -1, while 4 divided by -1 leaves a remainder of 0. Is this correct? Or should -1 divided by 4 leave a remainder of 3?

EDIT; I've just had another look at the puzzles and I notice that none of the non zero mod cells can contain a -1. So I guess the question is moot as far as those puzzles are concerned.

Author:  paulv66  [ Tue Aug 28, 2018 9:12 am ]
Post subject:  Re: what should volume IV have?

paulv66 wrote:
I have a question about the mod function where negative numbers are included. Two of the puzzles in the new book have numbers from -1 to +7 and include some cells with the mod function. I'm trying to figure out the logic where one of these cells includes -1.

if we look at a 'normal' cell with positive only values - say 2,5 - the answer can be either 1 or 2. If zero is included, the only answer can be 0 as division by 0 is undefined. If we take a cell with -1 and 4 (for example), -1 divided by 4 leaves a remainder of -1, while 4 divided by -1 leaves a remainder of 0. Is this correct? Or should -1 divided by 4 leave a remainder of 3?

EDIT; I've just had another look at the puzzles and I notice that none of the non zero mod cells can contain a -1. So I guess the question is moot as far as those puzzles are concerned.


I would be interested to know the answer to my question above. Should I post it as a new topic?

Author:  jpoos  [ Tue Aug 28, 2018 5:44 pm ]
Post subject:  Re: what should volume IV have?

Instead of thinking of the mod function as the remainder after division, you can also think of it in the following way:

Usually, one may say that 7 mod 3 is equal to 1, since 7 divided by 3 leaves a remainder of 1. Another way of saying this is: if we repeatedly substract 3 from 7 until we have a number between 0 and 3 (0 included), we get 1, since 7-3-3=1.
Similarly, you would say that -1 mod 3 is equal to 2, since if we repeatedly add 3 to -1 until we have a number between 0 and 3 (0 included), we get 2, since -1+3=2.
In this way, you can also pull negative numbers (or even fractions) through the mod function. Some more examples:
-6 mod 2 = 0
-10 mod 7 = 4
-6.5 mod 3 =2.5
3 mod 2.5 = 0.5
pi mod 2 = 1.1415926535897932384626433832795028841.....

Hopefully this clarifies things a bit.

Author:  beaker  [ Tue Aug 28, 2018 6:13 pm ]
Post subject:  Re: what should volume IV have?

That is going to help a lot for the weekly mod puzzles for me........thank you [smile]

Am going to print this and keep it with the mod tables

Author:  calcpnm  [ Tue Aug 28, 2018 6:20 pm ]
Post subject:  Re: mod and negative numbers

Thanks jpoos [thumbup]

Also, moved this from the "what should volume IV have" thread..

Author:  paulv66  [ Wed Aug 29, 2018 12:41 am ]
Post subject:  Re: mod and negative numbers

Thanks jpoos. That clears things up for me. I guess where the negative number is the denominator, repeated subtraction (i.e. double negative) of the denominator is required until you reach the lowest possible positive number? Therefore mod (-3,7) could be either 4 (where 7 is the denominator) or 2 (where -3 is the denominator). Or am I missing something?

Author:  jpoos  [ Thu Aug 30, 2018 11:46 am ]
Post subject:  Re: mod and negative numbers

paulv66 wrote:
Thanks jpoos. That clears things up for me. I guess where the negative number is the denominator, repeated subtraction (i.e. double negative) of the denominator is required until you reach the lowest possible positive number? Therefore mod (-3,7) could be either 4 (where 7 is the denominator) or 2 (where -3 is the denominator). Or am I missing something?


I don't think it's formally possible to have a negative number in the denominator, even though you could certainly extend the definition of the mod function to make that happen.
If you were to do this, you would say either 7 mod -3 = 1, since 7+(-3)+(-3)=1, or 7 mod -3 = -2, since 7+(-3)+(-3)+(-3)=-2, depending on the representatives you use.
The other calculation is definitely correct: -3 mod 7 = 4.

Author:  paulv66  [ Thu Aug 30, 2018 3:43 pm ]
Post subject:  Re: mod and negative numbers

Thanks jpoos. My reasoning where -3 is the denominator was faulty - too many double negatives!

Perhaps Patrick could clarify whether a negative denominator is permitted in mod cages?

Author:  paulv66  [ Mon Sep 17, 2018 3:54 pm ]
Post subject:  Re: mod and negative numbers

paulv66 wrote:
Thanks jpoos. My reasoning where -3 is the denominator was faulty - too many double negatives!

Perhaps Patrick could clarify whether a negative denominator is permitted in mod cages?


Patrick, can you clarify please? Also, is zero allowed as the denominator in a mod cell? For example, could you give 2 mod 0 as a solution for a cage with value mod 2? Or will a mod cage with zero in it always have to have value zero?

Author:  pnm  [ Mon Sep 17, 2018 7:28 pm ]
Post subject:  Re: mod and negative numbers

paulv66 wrote:
Patrick, can you clarify please? Also, is zero allowed as the denominator in a mod cell? For example, could you give 2 mod 0 as a solution for a cage with value mod 2? Or will a mod cage with zero in it always have to have value zero?


No, for a divisor zero the modulo operation is not defined, so it's not allowed.

(edit: I'll double-check this too, I've seen a definition where "x mod 0 = x", so maybe that would work)

When the divisor is negative, the result is "implementation dependent" as they say
(so it depends on the programming language used). In my case, the language is C++:
here the remainder has the same sign as the dividend.

But a puzzle generator script is written in Perl, which uses the convention
that the remainder has the same sign as the divisor..

I need to check if this causes any problems for the puzzle generation, will keep you posted.

Page 1 of 2 All times are UTC + 1 hour [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/