How to find a prime q such that it divides prime p-1? - divide

I would like to find a prime q such that it divides a prime p-1
E.g. p=23 (prime)
q|p-1 = 11|22
Are there any formula for doing so if prime p is not always a safe prime?

Related

How many numbers are divisible by n and m in the range [l, r](inclusive)?

I have four numbers n,m,l,r;
For a range between numbers l and r.
How can I find the total numbers whose % with n and m is 0 i.e. how many numbers between land r are divisible by n and m?
For a number to be divisible by both n and m, it must be divisible by the LCM (Least common multiple) of n and m. If a number is not divisible by the LCM of n and m, then it will not be divisible by at least one of n or m. Thus, we have a path to solving this problem: To determine the number of integers between l and r which are divisible by both n and m, we can count the number of integers between l and r which are divisible by LCM(n, m) which is r/lcm - l/lcm.
The LCM of two integers is calculated as n / GCD(n, m) * m. Many languages have a built-in GCD implementation, but if yours doesn't, you can certainly find one online. Best of luck!

What would be the time complexity for this naive method finding n prime numbers?

class Primes
def initialize
#primes = []
end
def prime_iterative(n)
i = 2
while #primes.size < n do
#primes << i if is_prime?(i)
i += 1
end
#primes
end
def is_prime?(n)
#primes.each { |prime| return false if n % prime == 0 }
true
end
end
primes = Primes. new
puts primes.prime_iterative 10
its finding n prime numbers not all primes less than n. I cant determine the upper bound
So this is trial division by primes, which is less efficient than the Sieve of Eratosthenes.
It checks the divisibility of each pair of primes, so certainly it is Ω(n²).
It's also O(n² log n), since the nth prime is (1 + o(1)) n log n, and pessimistically we divide each of the numbers up to the nth prime by each of the first n primes.
We can tighten this analysis to O(n²) (hence Θ(n²)) by observing that every composite c has a prime factor ≤ √c, so the primes take O(n²) time as noted above, and the composites take only O(n log n √(n/log n)) since each has a prime factor ≤ O(√(n log n)), which implies that we check O(√(n log n)/log √(n log n)) = O(√(n/log n)) primes.

Find if N! is divide by n^2

I have got an exercise which requires to find to write a program in which you should find if N! is divided by N^2.
1 ≤ N ≤ 10^9
I wanted to this with the easy way of creating factorial function and dividing it to the power of N but obviously it won't work.
Just algorithm or pseudo-code would be enough
For any n > 4, if n is a prime, then n! is not evenly divisible by n^2.
Here is simple explanation to support my argument:
After n! is divided by n, we are left with (n-1)! in the numerator that needs to be divided by n. So we need n or a multiple of n in the numerator in order for (n-1)! to be evenly divisible by n, which can never happen when n is prime.
While the above will always happen when n is a non-prime. Check it out for yourself by diving into a bit of Number Theory
Hope it helps!!!
Edit: Here is a simple Python code for the above. Complexity is O(sqrt(N)):
def checkPrime(n):
i = 2
while i<n**(1/2.0):
if n%i == 0:
return "Yes" # non-prime, so it's divisible
i = i + 1
return "No" # prime, so not divisible
def main():
n = int(raw_input())
if n==1:
print "Yes"
elif n==4:
print "No"
else:
print checkPrime(n)
main()
Input:
7
Output:
No
This is related to though easier than Wilson's Theorem which says that a number n > 1 is prime if and only if
(n-1)! = -1 (mod n)
This is algebraically equivalent to saying that n>1 is prime if and only if
n! = -n (mod n^2)
Furthermore, it is known and easy to prove that (to quote the Wikipedia article)
With the sole exception of 4, where 3! = 6 ≡ 2 (mod 4), if n is
composite then (n − 1)! is congruent to 0 (mod n).
Hence with the sole exception of 4, if n is composite, (n-1)! = 0 (mod n) hence n! = 0 (mod n^2) and if n is prime, n! = -n = n^2-n (mod n^2) hence n! isn't congruent to 0 in that case.
The full power of Wilson's theorem is needed if you want to show that for prime n, n! leaves a remainder of exactly n^2-n upon division by n^2. For this problem all you need to know is that it isn't zero.
In any event, you could just write a program which runs a primality check, although whether or not that would be considered a valid solution is up to whoever assigned the problem.

Minimize number of divisors of an integer within an interval

I have recently stumbled upon an algorithmic problem and I can't get the end of it. You're given a positive integer N < 10^13, and you need to choose a nonnegative integer M, such that the sum: MN + N(N-1) / 2 has the least number of divisors that lie between 1 and N, inclusive.
Can someone point me to the right direction for solving this problem?
Thank you for your time.
Find a prime P greater than N. There are a number of ways to do this.
If N is odd, then M*N + N*(N-1)/2 is a multiple of N. It must be divisible by any factor of N, but if we choose M = P - (N-1)/2, then M*N + N*(N-1)/2 = P*N, so it isn't divisible by any other integers between 1 and N.
If N is even, then M*N + N*(N-1)/2 is a multiple of N/2. It must be divisible by any factor of N/2, but if we choose M = (P - N + 1)/2 (which must be an integer), then M*N + N*(N-1)/2 = (P - N + 1)*N/2 + (N-1)*N/2 = P*N/2, so it isn't divisible by any other integers between 1 and N.

Calculating inverse Mod where Mod is not prime

I want to calculate value of
F(N) = (F(N-1) * [((N-R+1)^(N-R+1))/(R^R)]) mod M for given values of N,R and M.
Here A^B shows A power B and NOT any Bitwise operation
Here M need not to be prime.How to approach this question?Please help because if M was prime that it would not have beed so difficult to find inverse of R^R mod M.
But as M can be any value ranging from 1 to 10^9.I am not able to tackle this problem.
N can be between 1 and 10^5 and R is less than or equal to N.
Assuming you know somehow that the result of the division is an integer:
As N and R are small, you can do this by computing the prime factorisation of N-R+1 and R.
If we know that R=p^a...q^b then R^R = p^(Ra)...q^(Rb).
Similarly you can work out the power of each prime in (N-R+1)^(N-R+1).
Subtract the powers of the primes in R^R from the powers of the primes in (N-R+1)^(N-R+1) gives you the powers of each prime in the result.
You can then use a standard binary exponentiation routine to compute the result modulo M with no inverses required.

Resources