## Count common factors

find common factors of two numbers javascript
how to find common factors of two numbers
common factors of n numbers in python
how to find common factors of two numbers in python
common factors of two numbers c++
common divisors
greatest common divisor geeksforgeeks
common prime divisors of two numbers

I have written some code for calculating a count of common factors. Please help me remove anything unnecessary and shorten it.

Please tell me if any function in Python 3 can calculate common factors directly!

# a string of 2 numbers
# is input e.g. '10 15'

a,b=str(input()).split(' ')
a,b=int(a),int(b)
n=0
if a>=b:
lim=a
else:
lim=b
for num in range(1,lim):
if a%num==0 and b%num==0:
n+=1
print(n)


Count common factors, , the task is to find the count of common factors of two numbers where factors are prime. Examples: Input: A = 6, B = 12. Output: 2 2 and 3 are the� def num_divisors(n): """Return the number of divisors of n.""" count = 1 # Number of divisors factor = 2 # Candidate for prime factor of n # If n is not a prime number then it must have one factor # which is <= sqrt(n), so we try these first: while factor * factor <= n: if n % factor == 0: # factor is a prime factor of n, determine the exponent: exponent = 1 n /= factor while n % factor == 0: exponent += 1 n /= factor # factor^exponent is one term in the prime factorization of n, # this

So there's couple of things you can improve:

a,b=str(input()).split(' ')
a,b=int(a),int(b)

cdiv=[] #list to store common divisors
num=2
if(max(a,b)%min(a,b)==0):
c=min(a,b)
for i in range(2,int(c**0.5)+1):
while(c%i==0):
cdiv.append(i)
c/=i
if(c>1): cdiv.append(c)
else:
while(num<=min(a,b)//2):
while(a % num==0)&(b % num==0):
cdiv.append(num)
a/=num
b/=num
num+=1


(1) you indeed should focus on prime divisors.

(2) maximal potential divisor of any number x is x//2 (floor from half of x), so it's enough to look for divisors until min(a,b)//2 (cause we are looking only for common divisors).

(3) some of the prime divisors could be multiple times divisors - so also you should look for these.

(4) after confirming single number being divisor you can divide both numbers by it - by decreasing the numbers you make it easier to take modulo from it next time (you could probably also do it if divisor is just divisor for one of the numbers, but then you would have to check both numbers separately, which I'm not sure is worth doing in terms of computational overhead).

Count common prime factors of two numbers, A better solution is to calculate the greatest common divisor (gcd) of given two numbers, and then count divisors of that gcd. C++; Java; Python� Count common prime factors of two numbers; Print all numbers whose set of prime factors is a subset of the set of the prime factors of X; Count numbers from range whose prime factors are only 2 and 3; Find sum of exponents of prime factors of numbers 1 to N; K-Primes (Numbers with k prime factors) in a range

Basically you only need to iterate up to sqrt(a) because the inverse of every factor <= sqrt(a) gives you the one which is greater than sqrt(a).

See the following code example:

import math

def factors(a):
ans = set()
for i in range(1, int(math.sqrt(a)) + 1):
if a % i == 0:
ans.add(i)
ans.add(a // i)
return ans

def find_common_factors(a, b):
factors_a = factors(a)
factors_b = factors(b)
return factors_a.intersection(factors_b)


Common Divisors of Two Numbers, Given a set of numbers where all other numbers are multiple of smallest number, the program must find the count of the common factor C� You should take every number from 2 to max (a) and check if it is a factor/divisor of every number in a. If it is, you can increment count. or even better max (sqrt (a [i]) for a [i] in a) in pseudo-code-ish syntax.

Count of common factors – CrackChallenges, You got the loops in the wrong order. You are checking if there are n dividers of any of the numbers. Try swapping the loops. Then the count will� There can be several common factors, one common factor, or none. 3 Calculate: If there are no common factors then the greatest common factor is 1. If there's only one prime common factor, then that's your common factor.

Find the number of common factors for n numbers excluding "1" as , Explanation − common primes factors between 10 and 20 are 2 and 5 only. Input − x = 34 y = 12 Output − Common prime factor of two� Given an integer number and we have to count its all factors using java program. Example: Input: Enter the number : 45 Output: Number of factors of is : 6 Explanation: 45 is divisible by: 1, 3, 5, 9, 15, 45 Thus, factors will be 6 Program to count all factors of a number in java import java. util.

Count common prime factors of two numbers in C++, The largest common factor of two numbers is called their greatest common factor or highest common factor. ADVERTISEMENT. Example: What is� Lets be more precise. If a < b then simple test for 1. numbers<=a/2 2. whether a divide b 3. if any number is zero no need to find factors simply count 0 as shown here,

##### Comments
• Why does this have a VTC for lacking a description. Do you, the closer, not know what common factors are?
• Please tell me a bit about the map() function that you used in : a, b = map(int, input().split())
• @UtkarshSharma: The first argument is a function, which is applied to every item in the second argument (which is an iterable). Documented here: docs.python.org/3/library/functions.html#map.
• @UtkarshSharma: ... The result is an iterable, which is unpacked into a and b.
• Out of curiosity; is there a reason you don't remove if n % factor == 0: and the first n /= factor, and change exponent = 1 to exponent = 0?
• @Peilonrayz: Then a multiplication count *= exponent + 1 would also be done if factor is not a divisor. That would work because exponent = 0 in that case. (The real reason is that I translated this back from a JavaScript with a repeat-while loop :)
• You only have to iterate up to sqrt(min(a,b)) because if a or b is dividable by a number greater than its sqrt there also has to be a divider smaller than its sqrt.
• @WernerAltewischer: That is not correct, see also my comment at your answer. 10 and 25 have the common factor 5, which is larger than sqrt(min(10, 25)).
• @Werner Altewischer - you're right in terms of finding divisors for a single number, but if we're talking about common divisors - you might be interested only in exactly this higher divisor. (10, 25) is a very good counter-example.
• @GrzegorzSkibinski: Your solution seems to be problematic as well: 720 and 24 have the 8 common factors 1, 2, 3, 4, 6, 8, 12, 24, but your code produces [2, 2, 2].
• Thanks @Martin R I tweaked my answer - it's for the case when b%a==0
• I might be wrong, but there seems to be a problem with your solution. find_common_factors(10, 25) returns an empty list, but there are two common factors (1, 5).