Question

Two positive integers are amicable if each prime divisor of one is a divisor of the...

Two positive integers are amicable if each prime divisor of one is a divisor of the other. Example: 6 and 12 are amicable, since each prime divisor of 6 (2 and 3) also divides 12, and each prime divisor of 12 (again 2 and 3) divides 6. Another: 12 and 15 are not amicable, since a prime divisor of 15 (namely 5) does not divide 12. Find the sum of all amicable pairs whose two members are both less than or equal to a given n using Phyton.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python CODE ::

Execution ::...

Raw python code ::

_________________________________ amicable.py _________________________________

def isPrime(N): # Checks Prime or Not
   for i in range(2,N):
       if N%i == 0: # If there is a factor in between 2, n-1
           return False # then it is not a prime
   return True

def prime_factors(N): # Returns all prime factors
   fact = []
   for i in range(2,N+1): # 1 is not a prime factor so (2,N)
       if N%i == 0 :   # if it is factor
           if isPrime(i) :   # if it is a prime
               fact.append(i) # Appending to list
   return fact # Returning the list


def amicable(a,b): # Checks the amicable pair
   B = prime_factors(b)
   for f in B: # Checking all prime factors of 'b' for divisibility
       if a % f != 0: # with 'a'
           return False # if not returns False
   return True

n = int(input("Enter the n value : "))

print("amicable : ",end="")
Count = 0 ; Sum = 0
for a in range(1,n+1):
   for b in range(a+1,n+1):
       if amicable(a,b) and amicable(b,a):
           print("%s-%s,"%(a,b),end=" ")
           Count += 1 ; Sum += a+b
print("\n")
print("Number of amicable pairs :",Count)
print("Sum of amicable pairs :",Sum)

**************************************************************************************************************

####################################

||| Your Rating a LOT ||

####################################

Add a comment
Know the answer?
Add Answer to:
Two positive integers are amicable if each prime divisor of one is a divisor of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 15A and 15B (15) Let n E Z+,and let d be a positive divisor of...

    Part 15A and 15B (15) Let n E Z+,and let d be a positive divisor of n. Theorem 23.7 tells us that Zn contains exactly one subgroup of order d, but not how many elements Z has of order d. We will determine that number in this exercise. (a) Determine the number of elements in Z12 of each order d. Fill in the table below to compare your answers to the number of integers between 1 and d that are...

  • PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

    PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...

  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • Find the smallest positive integer that has precisely n distinct prime divisors. 'Distinct prime divisor'Example: the...

    Find the smallest positive integer that has precisely n distinct prime divisors. 'Distinct prime divisor'Example: the prime factorization of 8 is 2 * 2 * 2, so it has one distinct prime divisor. Another: the prime factorization of 12 is 2 * 2 * 3, so it has two distinct prime divisors. A third: 30 = 2 * 3 * 5, which gives it three distinct prime divisors. (n = 24 ⇒ 23768741896345550770650537601358310. From this you conclude that you cannot...

  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • Goldbach's Conjecture Python Loop through integers 4 through 100 for each number show two prime that...

    Goldbach's Conjecture Python Loop through integers 4 through 100 for each number show two prime that sum up to integer b)Show with  comments  how you found the prime numbers that add up to make each integer from the range 4 to 100 Example output: 4 = 2 + 2 6 = 3 + 3 etc. Hint:You are finding the even numbers from 4 through a 100 and finding the two prime numbers that  add to make the integer and printing it out like...

  • 10. Two integers r, y are called coprime if the only positive number that divides both...

    10. Two integers r, y are called coprime if the only positive number that divides both of them is 1. Provide an infinite sequence of distinct natural numbers 11, 12, 13, ... such that none of these numbers is a prime number, yet any pair of them are coprime with each other. Justify your answer. [5 marks]

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • Solve the following question using Matlab language only. Least common multiple (LCM) of two numbers is...

    Solve the following question using Matlab language only. Least common multiple (LCM) of two numbers is the smallest number that they both divide. For example, the LCM of 2 and 3 is 6, as both numbers can evenly divide the number 6. Find the LCM of two numbers using recursion Hint: You may assume that the first number is always smaller than the second number. Examplel First number for LCM:3 Second number for LCM 19 The LCM of 3 and...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT