热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查友好配对

检查友好配对原文:https://www.geeksforgee

检查友好配对

原文:https://www.geeksforgeeks.org/check-amicable-pair/

友好数是两个不同的相关数,因此每个数的适当因子的和等于另一个数。(一个数的适当除数是该数的正因子,而不是该数本身。
例:

Input : x = 220, y = 284
Output : Yes
Proper divisors of 220 are 1, 2, 4, 5,
10, 11, 20, 22, 44, 55 and 110\. Sum of
these is 284\. Proper divisors of 284
are 1, 2, 4, 71 and 142 with sum 220.
Input : 1 2
Output :No

逻辑很简单。我们比较两个数的适当除数的和,并将一个数的和与另一个数的和进行比较。

C++


// CPP program to check if two numbers are
// Amicable or not.
#include
using namespace std;
// Function to calculate sum of all 
// proper divisors of a given number
int divSum(int n)
{
    // Sum of divisors
    int result = 0;
    // find all divisors which divides 'num'
    for (int i = 2; i <= sqrt(n); i++)
    {
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
            // if both divisors are same
            // then add it once else add
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n/i);
        }
    }
    // Add 1 and n to result as above loop
    // considers proper divisors greater 
    // than 1.
    return (result + 1);
}
// Returns true if x and y are Amicable
// else false.
bool areAmicable(int x, int y)
{
    if (divSum(x) != y)
       return false;
    return (divSum(y) == x);
}
int main() {
    int x = 220, y = 284;
    if (areAmicable(x, y))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java 语言(一种计算机语言,尤用于创建网站)


// JAVA program to check if two numbers are
// Amicable or not.
import java.io.*;
class GFG 
{
    // Function to calculate sum of all 
    // proper divisors of a given number
    static int divSum(int n)
    {
        // Sum of divisors
        int result = 0;
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
        // Add 1 and n to result as above loop
        // considers proper divisors greater 
        // than 1.
        return (result + 1);
    }
    // Returns true if x and y are Amicable
    // else false.
    static boolean areAmicable(int x, int y)
    {
        if (divSum(x) != y)
        return false;
        return (divSum(y) == x);
    }
    public static void main (String[] args) 
    {
        int x = 220, y = 284;
        if (areAmicable(x, y))
        System.out.println( "Yes");
        else
        System.out.println("No");
    }
} 
// This code is contributed by vt_m.


Python 3


# Python program to check 
# if two numbers are
# Amicable or not.
import math
# def to calculate sum 
# of all proper divisors
# of a given number
def divSum(n) :
    # Sum of divisors
    result = 0
    # find all divisors 
    # which divides 'num'
    for i in range(2, int(math.sqrt(n)) + 1) :
        # if 'i' is 
        # divisor of 'n'
        if (n % i == 0) :
            # if both divisors are same
            # then add it once else add
            # both
            if (i == int(n / i)) :
                result = result + i
            else :
                result = result + 
                         (i + int(n / i))
    # Add 1 and n to result 
    # as above loop considers
    # proper divisors greater 
    # than 1.
    return (result + 1)
# Returns true if x and y 
# are Amicable else false.
def areAmicable(x, y) :
    if (divSum(x) != y) :
        return False
    return (divSum(y) == x) 
# Driver Code
x = 220
y = 284
if (areAmicable(x, y)) :
    print ("Yes")
else :
    print ("No")
# This code is contributed by 
# Manish Shaw(manishshaw1)


C


// C# program to check if two numbers are
// Amicable or not.
using System;
class GFG 
{
    // Function to calculate sum of all 
    // proper divisors of a given number
    static int divSum(int n)
    {
        // Sum of divisors
        int result = 0;
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.Sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
        // Add 1 and n to result as above loop
        // considers proper divisors greater 
        // than 1.
        return (result + 1);
    }
    // Returns true if x and y are Amicable
    // else false.
    static bool areAmicable(int x, int y)
    {
        if (divSum(x) != y)
        return false;
        return (divSum(y) == x);
    }
    public static void Main () 
    {
        int x = 220, y = 284;
        if (areAmicable(x, y))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine("No");
    }

// This code is contributed by vt_m.


服务器端编程语言(Professional Hypertext Preprocessor 的缩写)



// PHP program to check if two
// numbers are Amicable or not.
// Function to calculate sum of all 
// proper divisors of a given number
function divSum( $n)
{
    // Sum of divisors
    $result = 0;
    // find all divisors 
    // which divides 'num'
    for ($i = 2; $i <= sqrt($n); $i++)
    {
        // if 'i' is divisor of 'n'
        if ($n % $i == 0)
        {
            // if both divisors are same
            // then add it once else add
            // both
            if ($i == ($n / $i))
                $result += $i;
            else
                $result += ($i + $n / $i);
        }
    }
    // Add 1 and n to result 
    // as above loop considers
    // proper divisors greater 
    // than 1.
    return ($result + 1);
}
// Returns true if x and y 
// are Amicable else false.
function areAmicable($x, $y)
{
    if (divSum($x) != $y)
        return false;
    return (divSum($y) == $x);
}
    // Driver Code
    $x = 220;
    $y = 284;
    if (areAmicable($x, $y))
        echo "Yes";
    else
        echo "No";
// This code is contributed by anuj_67.
?>


java 描述语言


<script>
    // Javascript program to check if two 
// numbers are Amicable or not
// Function to calculate sum of all 
// proper divisors of a given number 
function divSum(n) 

    // Sum of divisors 
    let result = 0
    // find all divisors 
    // which divides 'num' 
    for (let i = 2; i <= Math.sqrt(n); i++) 
    { 
        // if 'i' is divisor of 'n' 
        if (n % i == 0) 
        { 
            // if both divisors are same 
            // then add it once else add 
            // both 
            if (i == (n / i)) 
                result += i
            else
                result += (i + n / i)
        } 
    } 
    // Add 1 and n to result 
    // as above loop considers 
    // proper divisors greater 
    // than 1
    return (result + 1)

// Returns true if x and y 
// are Amicable else false
function areAmicable(x, y) 

    if (divSum(x) != y) 
        return false
    return (divSum(y) == x)

    // Driver Code 
    let x = 220
    let y = 284
    if (areAmicable(x, y)) 
        document.write("Yes")
    else
        document.write("No")
// This code is contributed by _saurabh_jaiswal
script>

输出:

Yes


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
闲云野鹤-男主角_185
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有