How to perform a reverse mod? [duplicate] - math

This question already has answers here:
how to calculate reverse modulus
(5 answers)
Closed 5 years ago.
If I have an equation like (x+c) mod y = z and I need to solve for y, how would I go about doing that?
Apologies if this is more math than programming.

No simple formula exists. If a mod n = r then n divides a-r and 0 <= r < n. Candidate n can be found by factoring a - r and finding divisors which are larger than r. Factoring is a much-studied but non-trivial problem. Pick your favorite factoring algorithm. Unless a-r is prime, there won't be a unique solution (unless r is larger than any proper-divisor of a-r).

Related

0.5<0.5 returns TRUE in R? [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 2 years ago.
I came across a strange thing in R programming. When I simulate a sequence and want to judge whether the element is less than 0.5,
t=(1:1440)/1440
x=(t[720]-t[648])/0.1
x
#output:[1] 0.5
x<1/2
#output:[1] TRUE
x=0.5
x<1/2
#output:[1] FALSE
The two results are completely opposite and obviously the second result is what I want. Can anybody help me?
Floating point arithmetic is not exact in R, and the value you expect to be numerically exact to 0.5 may in fact be slightly more (or less). One possible workaround here would be to use rounding:
t <- (1:1440)/1440
x <- (t[720]-t[648]) / 0.1
round(x, 1) < 0.5

R - Equivalent inputs resulting in different outputs for a sequence [duplicate]

This question already has an answer here:
Why does the vector gets expanded in the loop
(1 answer)
Closed 6 years ago.
I am running into some behaviour with R that I find confusing. Does anyone have any insight into what is going on here?
Define two objects
i <- 5
nr <- 10
So i + 2 and nr + 1
> i+2
[1] 7
> nr+1
[1] 11
So to create a sequence from 7 to 11 I could do this:
7:11
But my question why does this not produce the same result?
i+2:nr+1
We already established above that it's input numbers are equivalent. Obviously I'm missing something here but I just don't know what it is.
You have just discovered the prime R gotcha, namely: 1:n-1 produces the sequence 0, 1, 2, ..., n-1.
To obtain what you desire, wrap the expressions in brackets:
1:(n-1)
or use
seq.int(1, n-1)
The reason for the issue is operator precedence - ?Syntax`

Unexpected behaviour of greater and less than [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are these numbers not equal?
Can anyone explain to me, why R answers FALSE in the following case?
(1-0.9)>=0.1
How can I get TRUE for comparisons of that kind?
This has to do with floating point precision. There is in essence an infinite amount of floating points, do representing them in the computer can only be done discretely, and thus with limited precision. To take this limited precision into account, use all.equal to make the comparison. As #RomainFracois said, this is very frequently asked question in R.
This is the classic R FAQ 7.31. You need all.equal
You could create your own binary operators to do what you're after and store them in your .Rprofile:
`%>=%` <- function(x, y) all.equal(x, y) | x > y
`%<=%` <- function(x, y) all.equal(x, y) | x < y
c(1-.9)>=.1
c(1-.9)%>=% .1

Probability of n-bit integers [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
random a 512-bit integer N that is not a multiple of 2, 3, or 5
I have a question
for a random 512-bit integer n that isn't a multiple of 2,3, or 5 what is the chance that n is prime? what about that n is composite but fools the fermat primality test? what about that it is composite but doesn't fool the fermat primality test?
Since this is definitely a homework problem, I'll point you at the Prime Number Theorem, which should give you the probability that any large number is prime.
From there, modify the probability with your new information about composite numbers that have been eliminated (Hint: Think about how the problem space shrinks).
Best of luck!

primes and logarithms [theory] [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I know this is not a mathematical forum but given the bright minds that participate here, i am sure that this question is of interest nevertheless. How would you develop and explain the following statement:
"we can convert the product of a set
of primes into a sum of the logarithms
of the primes by applying logarithms
to both parts of this conjecture"
log(a * b) = log(a) + log(b)
thanks for that OrangeDog and John!
re benefit of introducing logs, OrangeDog is right indeed. It is specific to an exercise from an MIT OpenCourse class. Here's the full details:
There is a cute result from number
theory that states that for
sufficiently large n the product of
the primes less than n is less than or
equal to e^n and that as n grows,
this becomes a tight bound (that is,
the ratio of the product of the primes
to e^n gets close to 1 as n grows).
Computing a product of a large number
of prime numbers can result in a very
large number, which can potentially
cause problems with our computation.
[note: this is what John was referring
to] So we can convert the product of a
set of primes into a sum of the
logarithms of the primes by applying
logarithms to both parts of this
conjecture. In this case, the
conjecture above reduces to the claim
that the sum of the logarithms of all
the primes less than n is less than n,
and that as n grows, the ratio of this
sum to n gets close to 1.
EDIT
given these statements i am, however, unsure about how to apply them i.e.
how do we go from here:
2 x 3 x 5 <= e^7
to
"applying
logarithms to both parts of this
conjecture."
EDIT 2
got it...
2 x 3 x 5 <= e^7
knowing that logarithms are the opposite of powers we can say:
log(2x3x5) <= 7
which is also the same as:
log(2)+log(3)+log(5) <= 7
this only starts to show its "value" when n (in this case 7) gets larger i.e. the 1000th prime or higher

Resources