给定两个数字A和B,任务是找到两个数字A和B的公因子之和。数字A和B小于10 ^ 8。
例子:
Input: A = 10, B = 15
Output: Sum = 6
The common factors are 1, 5, so their sum is 6
Input: A = 100, B = 150
Output: Sum = 93
天真的方法:从i = 1迭代到A和B的最小值,并检查i是否是A和B的因数。如果i是A和B的因数,则将其加和。在循环末尾显示总和。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// print the sum of common factors
int sum(int a, int b)
{
// sum of common factors
int sum = 0;
// iterate from 1 to minimum of a and b
for (int i = 1; i <= min(a, b); i++)
// if i is the common factor
// of both the numbers
if (a % i == 0 && b % i == 0)
sum += i;
return sum;
}
// Driver code
int main()
{
int A = 10, B = 15;
// print the sum of common factors
cout << "Sum = " << sum(A, B) << endl;
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class GFG {
// print the sum of common factors
static int sum(int a, int b)
{
// sum of common factors
int sum = 0;
// iterate from 1 to minimum of a and b
for (int i = 1; i <= Math.min(a, b); i++)
// if i is the common factor
// of both the numbers
if (a % i == 0 && b % i == 0)
sum += i;
return sum;
}
// Driver code
public static void main (String[] args) {
int A = 10, B = 15;
// print the sum of common factors
System.out.print("Sum = " + sum(A, B));
}
}
// This code is contributed by shs..
Python 3
# Python 3 implementation of
# above approach
# print the sum of common factors
def sum(a, b):
# sum of common factors
sum = 0
# iterate from 1 to minimum of a and b
for i in range (1, min(a, b)):
# if i is the common factor
# of both the numbers
if (a % i == 0 and b % i == 0):
sum += i
return sum
# Driver Code
A = 10
B = 15
# print the sum of common factors
print("Sum =", sum(A, B))
# This code is contributed
# by Akanksha Rai
C#
// C# implementation of above approach
using System;
class GFG {
// print the sum of common factors
static int sum(int a, int b)
{
// sum of common factors
int sum = 0;
// iterate from 1 to minimum of a and b
for (int i = 1; i <= Math.Min(a, b); i++)
// if i is the common factor
// of both the numbers
if (a % i == 0 && b % i == 0)
sum += i;
return sum;
}
// Driver code
public static void Main () {
int A = 10, B = 15;
// print the sum of common factors
Console.WriteLine("Sum = " + sum(A, B));
}
}
// This code is contributed by shs..
PHP
Javascript
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
int main()
{
int a = 10, b = 15;
cout << "Sum = " << sumcommDiv(a, b);
return 0;
}
Java
//Java implementation of above approach
import java.io.*;
class GFG {
// Function to calculate gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
static int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
public static void main (String[] args) {
int a = 10, b = 15;
System.out.println("Sum = " + sumcommDiv(a, b));
}
}
Python3
# Python 3 implementation of above approach
from math import gcd,sqrt
# Function to calculate all common divisors
# of two given numbers
# a, b --> input integer numbers
def sumcommDiv(a, b):
# find gcd of a, b
n = gcd(a, b)
# Find the sum of divisors of n.
sum = 0
N = int(sqrt(n))+1
for i in range(1,N,1):
# if 'i' is factor of n
if (n % i == 0):
# check if divisors are equal
if (n / i == i):
sum += i
else:
sum += (n / i) + i
return sum
# Driver program to run the case
if __name__ == '__main__':
a = 10
b = 15
print("Sum =",int(sumcommDiv(a, b)))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of above approach
using System;
public class GFG{
// Function to calculate gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
static int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= Math.Sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
static public void Main (){
int a = 10, b = 15;
Console.WriteLine("Sum = " + sumcommDiv(a, b));
}
}
PHP
input integer numbers
function sumcommDiv($a, $b)
{
// find gcd of a, b
$n = gcd($a, $b);
// Find the sum of divisors of n.
$sum = 0;
for ($i = 1; $i <= sqrt($n); $i++) {
// if 'i' is factor of n
if ($n % $i == 0) {
// check if divisors are equal
if ($n / $i == $i)
$sum += $i;
else
$sum += ($n / $i) + $i;
}
}
return $sum;
}
// Driver program to run the case
$a = 10;
$b = 15;
echo "Sum = " , sumcommDiv($a, $b);
?>
Javascript
输出:
Sum = 6
一种有效的方法是使用两个数的公共除数中使用的相同概念。计算给定两个数字的最大公约数(gcd),然后找到该gcd的公约数之和。
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
int main()
{
int a = 10, b = 15;
cout << "Sum = " << sumcommDiv(a, b);
return 0;
}
Java
//Java implementation of above approach
import java.io.*;
class GFG {
// Function to calculate gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
static int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
public static void main (String[] args) {
int a = 10, b = 15;
System.out.println("Sum = " + sumcommDiv(a, b));
}
}
Python3
# Python 3 implementation of above approach
from math import gcd,sqrt
# Function to calculate all common divisors
# of two given numbers
# a, b --> input integer numbers
def sumcommDiv(a, b):
# find gcd of a, b
n = gcd(a, b)
# Find the sum of divisors of n.
sum = 0
N = int(sqrt(n))+1
for i in range(1,N,1):
# if 'i' is factor of n
if (n % i == 0):
# check if divisors are equal
if (n / i == i):
sum += i
else:
sum += (n / i) + i
return sum
# Driver program to run the case
if __name__ == '__main__':
a = 10
b = 15
print("Sum =",int(sumcommDiv(a, b)))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of above approach
using System;
public class GFG{
// Function to calculate gcd of two numbers
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
static int sumcommDiv(int a, int b)
{
// find gcd of a, b
int n = gcd(a, b);
// Find the sum of divisors of n.
int sum = 0;
for (int i = 1; i <= Math.Sqrt(n); i++) {
// if 'i' is factor of n
if (n % i == 0) {
// check if divisors are equal
if (n / i == i)
sum += i;
else
sum += (n / i) + i;
}
}
return sum;
}
// Driver program to run the case
static public void Main (){
int a = 10, b = 15;
Console.WriteLine("Sum = " + sumcommDiv(a, b));
}
}
的PHP
input integer numbers
function sumcommDiv($a, $b)
{
// find gcd of a, b
$n = gcd($a, $b);
// Find the sum of divisors of n.
$sum = 0;
for ($i = 1; $i <= sqrt($n); $i++) {
// if 'i' is factor of n
if ($n % $i == 0) {
// check if divisors are equal
if ($n / $i == $i)
$sum += $i;
else
$sum += ($n / $i) + $i;
}
}
return $sum;
}
// Driver program to run the case
$a = 10;
$b = 15;
echo "Sum = " , sumcommDiv($a, $b);
?>
Java脚本
输出:
Sum = 6
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。