Modular arithmetic using fractions - modulo

I'm stuck on this cryptography problem using multiplication of a whole number and a fraction mod 10.
Here is the equation:
7 * (4/11) mod 10 =?
I know I am supposed to convert this to an integer since the mod operator does not work with fractions, but I cannot figure this one out. Obviously,
7 * (4/11) = 28/11,
but I cannot get the mod 10 of a fraction. The instructor wants the exact answer, not a decimal. Any help would be greatly appreciated!

Have a look here: "Is it possible to do modulo of a fraction" on math.stackexchange.com.
One natural way to define the modular function is
a (mod b) = a − b ⌊a / b⌋
where ⌊⋅⌋ denotes the floor function. This is the approach used in the influential book Concrete Mathematics by Graham, Knuth, Patashnik.
This will give you 1/2(mod3)=1/2.
To work through your problem, you have a = 7 * (4/11) = 28/11, and b = 10.
a / b = (28/11)/10 = 0.25454545...
⌊a/b⌋ = 0
b ⌊a/b⌋ = 0 * 0 = 0
a - b ⌊a/b⌋ = 28/11 - 0 = 28/11
This means your answer is 28/11.
Wolfram Alpha agrees with me and gives 28/11 as the exact result. Google also agrees, but gives it as a decimal, 2.54545454.....
A fraction is an exact answer and not a decimal.

8
8 is the correct answer indeed.
7*4/11 mod 10 means we're looking at 7*4*x mod 10 where x is the modular inverse of 11 modulo 10, which means that 11*x mod 10 = 1.
This is true for x=1 (11*1 mod 10 = 1)
So 7*4*x mod 10 becomes 7*4*1 mod 10 which is 28 mod 10 = 8

I can speculate that the notation is wrong, and that the whole expression is supposed to be evaluated in mod 10 at each intermediate stage. Since ( 11 mod 1 ) is 1, then answer is (7 * 4) mod 10 = 8.
Imagine a calculator with support only for the ones digit.
I'm not saying this is the right answer, I agree 28/11 is the right answer as given, but I am trying to get into the head of the professor. This is common in cryptography, where every calculation is performed mod 2 ^ 256 or so.

This is how the original question probably should have been written, as this has a different meaning. When the (mod 10) is written at the end, it means that each term is evaluated with an implied mod 10 operation.
The problem is a bit weird, as the modulo value of 10 is not general purpose, because it is not prime. For example, the following can not be evaluated because 1/2 mod 10 is not defined, because 2 and 10 are not coprime.

So, here is the correct answer from the instructor. I have no idea how he came up with this:
7 4/11 mod 10 = ((7 4) mod 10)(11−1 mod 10) mod 10
= (28 mod 10)(1 mod 10) mod 10
= (8)(1) mod 10
= 8 mod 10

Using Python:
from fractions import Fraction
from math import fmod
print (fmod(Fraction(28, 11), 10))
The result will be 2.545454545454. So I guess 8 is wrong.

Related

Converting some values in a variable from minutes to hours while keeping the rest the same

In a questionnaire I asked people for how long they ususally sleep a night. Now replys were supposed to be in a h.mm format but were accidently set to h only. This is why some participants gave their sleep duration in minutes or simply wrote "830" for 8 hours 30 minutes. Now I wanted to correct for both variations and tried this first:
RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)(200 thru 600 = (Sleep/60))
INTO Sleep_rek.
RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
INTO PSQI_K3_SleepDuration.
Execute.
Since it didn't work and I thought maybe Recode can't do that, I tried this instead:
RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)
INTO Sleep_rek.
IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60).
RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
INTO PSQI_K3_SleepDuration.
Execute.
However this threw an error as well:
The code is incomplete, check for missing operants, invalid operants,
non-matching paranthesis or too long strings.
The PSQI_K3_SleepDuration variable in the end was computed but anyone with a 300 to 600 value is still a missing value.
Can anyone tell me how to put it so it will work?
I can see a few errors in your syntax - see if it works once you've corrected them.
Indeed, recode .... (200 thru 600 = (Sleep/60)) can not work, you can't use a calculation as the target value.
recode ... (5, 6= 1) should be (5 6= 1) instead (no comma needed).
IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60) has a couple of errors: thru ia a subcommand for recode, can't be used like this, also can't use "sysmis" this way. Corrected version:
IF (Sleep>= 300 and Sleep<=600) and missing(Sleep_rek) Sleep_rek = (Sleep/60).
Your last recode is fine syntax-wise, but the ranges you are using seem to be wrong - since you'll be dividing numbers from 300 to 600 in 60, you will only get fractions in values 5 to 10. Yet the recode covers fractions in numbers under 4.5 and doesn't cover them in values between 5 to 7.5 (except specific values: 5, 6, 6.5, 7).

Is there a long method operation for modulo if mod or % is not a supported function/operator?

This is related to the Zeller's Congruence algorithm where there is a requirement to use Modulo to get the actual day of an input date. However, in the software I'm using which is Blueprism, there is no modulo operator/function that is available and I can't get the result I would hope to get.
In some coding language (Python, C#, Java), Zeller's congruence formula were provided because mod is available.
Would anyone know a long method of combine arithmetic operation to get the mod result?
From what I've read, mod is the remainder result from two numbers. But
181 mod 7 = 6 and 181 divided by 7 = 25.857.. the remainder result are different.
There are two answers to this.
If you have a floor() or int() operation available, then a % b is:
a - floor(a/b)*b
(revised to incorporate Andrzej Kaczor's comment, thanks!)
If you don't, then you can iterate, each time subtracting b from a until the remainder is less than b. At that point, the remainder is a % b.

Unsatisfiable formula ? maybe wrong syntax?

Formula which I wanna solve looks like this in C:
#define foo(w,x,y) ((((w)*(x)*(y)+31) & ~31) / 8)
WORD w,x,y,z;
y = 24;
if( (foo(w,x,y) * z) == -1 )
printf("yeah!");
I rewrite it to z3py in the following way:
from z3 import *
w= BitVec('w',16)
x= BitVec('x',16)
z= BitVec('z',16)
y= BitVecVal(24,16)
solve( (UDiv( (w*x*y+31) & ~31, 8 )) * z == 0xffffffff)
Any suggestions ?
PS: Please notice that trying to solve formula in that form:
solve( (UDiv( (w*x*y+31), 8 )) * z == 0xffffffff)
is possible, so I can't belive that bit-wise operation causes this formula Unsatisfiable.
I don't see anything wrong in Z3 behavior. Why do you think the formula should be satisfiable?
A = (w*x*y+31) & ~31 -- imply that the 5 rightmost bits will be always zero
B = UDiv( A & ~31, 8 ) (equal to a logical shift right by 3) -- imply that the 2 rightmost bits will be always zero.
C = B * z -- this will always have the 2 rightmost bits zero
c == 0xffffffff -- that's impossible
If you change the constant to 0xfffffffc, then you'll get a solution.
The C program doesn't print anything for me. And Z3 says "no solution". So, it's consistent.
Does your C program prints "yeah!"? If so, aren't you on a big-endian machine? I've tried your example in an x86 machine.

Lua: converting from float to int

Even though Lua does not differentiate between floating point numbers and integers, there are some cases when you want to use integers. What is the best way to covert a number to an integer if you cannot do a C-like cast or without something like Python's int?
For example when calculating an index for an array in
idx = position / width
how can you ensure idx is a valid array index? I have come up with a solution that uses string.find, but maybe there is a method that uses arithmetic that would obviously be much faster. My solution:
function toint(n)
local s = tostring(n)
local i, j = s:find('%.')
if i then
return tonumber(s:sub(1, i-1))
else
return n
end
end
You could use math.floor(x)
From the Lua Reference Manual:
Returns the largest integer smaller than or equal to x.
Lua 5.3 introduced a new operator, called floor division and denoted by //
Example below:
Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>12//5
2
More info can be found in the lua manual
#Hofstad is correct with the math.floor(Number x) suggestion to eliminate the bits right of the decimal, you might want to round instead. There is no math.round, but it is as simple as math.floor(x + 0.5). The reason you want to round is because floats are usually approximate. For example, 1 could be 0.999999996
12.4 + 0.5 = 12.9, floored 12
12.5 + 0.5 = 13, floored 13
12.6 + 0.5 = 13.1, floored 13
local round = function(a, prec)
return math.floor(a + 0.5*prec) -- where prec is 10^n, starting at 0
end
why not just use math.floor()? it would make the indices valid so long as the numerator and denominator are non-negative and in valid ranges.

Erlang - Math and Code Elegance Question

Its a very tiny bit of math and more a question of the most efficient, most elegant way possible.
If I give an integer such as
1.50
or
1.22
or
10.99
How can I get rid of the number to the left of the decimal and output the right side as an integer or float, for example
.50 or 50
.22 or 22
.99 or 99
It matters what the fastest way to do it is. I would rather not turn it into a string if possible.
Thanks for the help.
BR
First of all, 1.50 is not an integer, it's a float. If there's a decimal, it's not an integer. That said, you could get the two digits after the decimal like this:
Precision = 100.
Value = 1.50.
Decimal = trunc(Value * Precision) rem Precision.
If you need more digits, alter the precision variable.
One way is via trunc(Number):
1> X = 10.23 .
10.23
2> Y = X - trunc(X) .
0.23000000000000043
% To get an integer out of the float, you can use `trunc` twice, but you'll have
% to adjust your number first ...
3> Precision = 100 .
100
4> Z = trunc(Y * Precision) .
23
% In one statement:
5> Z = trunc((X - trunc(X)) * Precision) .

Resources