Number of Divisors [closed] - math

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 10 years ago.
Improve this question
How to find the Number of divisors of a number 'n' that are also divisible by another number 'k' without looping through all the divisors of n?
I tried the following:
Stored powers of all prime factors of n in an associative array A and did similarly for k, stored the powers of all primes factors in array B.
ans = 1
for a in A: // Here a is the prime factor and A[a] gives its power
ans *= if( a is present in B ) ? 1 : A[a] + 1
print ans
Note : It is not homework.

To find the number of divisors of n that are divisible by k:
if k is not a divisor of n, the number is 0,
otherwise, it's the number of divisors of n/k.
If d is a divisor of n that is divisible by k, then d/k is a divisor of n/k. Conversely, if e is a divisor of n/k, then e*k is a divisor of n that is divisible by k.
Your code
ans = 1
for a in A: // Here a is the prime factor and A[a] gives its power
ans *= if( a is present in B ) ? 1 : A[a] + 1
print ans
calculates the number of divisors of n that are coprime to k.

if the divisor is a prime number, then there is no other way than looping through all divisors. Otherwise RSA would not be a save crypto algorithem.

Related

How many ways to get a sum of consecutive whole numbers that total x? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would one go to programmatically tackle these questions:
How many different ways are there to write:
a) 2021 as the sum of consecutive whole numbers?
b) x as the sum of consecutive whole numbers?
Supposing the order of the numbers isn't important, there are 8518741657943308344041302580996941768179250799 ways to write 2021 as a sum of positive integers. It's called the partition number. There isn't a simple formula, but there is a recurrence relation that allows to calculate a lot of values.
The function is implemented in Python's sympy:
from sympy import partition
print(partition(2021))
Now, for the new question, to write 2021 as the sum of consecutive integers: n + n+1 + ... + m-1 + m = 2021. First note that negative values for n don't add anything interesting. When n < -m, the sum would clearly be negative. Otherwise, the negative values just "eat up" the corresponding positive values, and you get the same sum as a positive solution.
The sum of the numbers from 1 to m is tri(m)=m*(m+1)/2, (the triangular number). The sum of the numbers from n to m is just tri(m)-tri(n-1). These equations can be solved by a SAT/SMT solver such as Z3:
from z3 import Int, Solver, And, Or, sat
s = Solver()
m = Int('m')
n = Int('n')
s.add(And(n > 0, n < 2022))
s.add(n <= m)
s.add(And(m > 0, m < 2022))
s.add(m * (m + 1) - n * (n - 1) == 2021 * 2)
while s.check() == sat:
mi = s.model()[m].as_long()
ni = s.model()[n].as_long()
if ni > mi - 30:
print("+".join([str(i) for i in range(ni, mi + 1)]), end="=")
else:
print("+".join([str(i) for i in range(ni, ni + 6)]), "+...+",
"+".join([str(i) for i in range(mi - 4, mi + 1)]), end="=", sep="")
print(sum([i for i in range(ni, mi + 1)]))
s.add(Or(m != mi, n != ni))
Output:
26+27+28+29+30+...+64+65+66+67+68=2021
20+21+22+23+24+...+62+63+64+65+66=2021
1010+1011=2021
2021=2021
So, for 2021 there are 4 positive solutions (and 4 corresponding solutions that would include the negative numbers from 1-n to m).

How does using log10 correctly calculate the length of a integer? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
int length = (int) floor( log10 (float) number ) + 1;
My question is essentially a math question: WHY does taking the log10() of a number, flooring that number, adding 1, and then casting it into an int correctly calculate the length of number?
I really want to know the deep mathematical explanation please!
For an integer number that has n digits, it's value is between 10^(n - 1)(included) and 10^n, and so log10(number) is between n - 1(included) and n. Then the function floor cuts down the fractional part, leaves the result as n - 1. Finally, adding 1 to it gives the number of digits.
Consider that a four-digit number x is somewhere between 1000 <= x < 10000. Taking the log base 10 of all three components gives 3.000 <= log(x, 10) < 4.000. Taking the floor (or int) of each component and adding one gives 4 <= int(log(x, 10))+1 <= 4.
Ignoring round-off error, this gives you the number of digits in x.

Number Theory - Factors, HCF and LCM [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
30, 40 and 'n' are such that every number is a factor of the product of other 2 number. If 'n' is a positive
integer , what is the difference between the maximum value of 'n' and the minimum value of 'n'?
Now, since it says that n is a factor of the product of the other 2 numbers, the max value that n can take is 1200 right?
i guess the hcf will give the minimum value of n
Listing the factors of 30 and 40
30 -> 1,2,3,5,6,10,15,30
40 -> 1,2,4,5,8,10,20,40
hcf(30,40) -> 10
Therfore, the difference is 1200-10 => 1190..
But the answer that is given is 1188...where am i going wrong?
Your approach is wrong. The greatest common divisor of 30 and 40 is not your smallest n.
You are looking for the smallest integer n > 0 that satisfies 40*n = 0 (mod 30) and 30*n = 0 (mod 40).
For the first equation, the result is n_1 = 3. For the second equation, we get n_2 = 4. The smallest n to satisfy both equations is the least common multiple of n_1 and n_2 -- in this case, n = 12.
hcf(30,40) -> 12
30=2*3*5
40=2*2*2*5
So, hcf(30,40) -> 3*2*2=12

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

What is the meaning of ∃? [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 11 years ago.
Improve this question
Reading a book on algorithms. Can someone explain the meaning of the mathematical symbol ∃?
It is called a quantifier. It means "there exists".
When used in an expression such as
∃x s.t. x > 0
It means "There exists a number x such that x is greater than 0."
Its counterpart is ∀, which means "for all". It's used like this:
∀x, x > 0
Which means "For any number x, it is greater than 0."
It is the "existential quantifier" as opposed to the upside-down A (∀) which means "universal quantifier." It should be read as "there exists" or "for some". It is a predication that means that some relation or property holds true for at least one object in the domain.
Examples:
An integer n is composite if ∃ integer m such that m > 1 and m < n with n divisible by m.
An integer n is prime if ∀ integer m such that m > 1 and m < n it is true that n is not divisible by m.
A function f is continuous on a metric space (X, d) if ∀x∀ε>0∃δ>0 | ∀y d(x, y) < δ => d(f(x), f(y)) < ε
More Info on Predicate Logic
It is called existential quantifier and being followed by x, it means there exists at least one x
For future reference, wikipedia has a table of mathematical symbols, with an explanation of the meaning(s) of each one.

Resources