寻找 1 到 100 之间的完美数字

发布于 2024-08-08 02:32:26 字数 74 浏览 3 评论 0原文

如何生成 1 到 100 之间的所有完全数?

完美数是等于其真因数之和的正整数。例如,6(=1+2+3)是一个完全数。

How can I generate all perfect numbers between 1 and 100?

A perfect number is a positive integer that is equal to the sum of its proper divisors. For example, 6(=1+2+3) is a perfect number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窝囊感情。 2024-08-15 02:32:26

所以我怀疑 Frank 正在 Prolog 中寻找答案,是的,它确实闻起来相当

为了好玩,我决定写下我的答案。我花了大约 50 行。

这是我的谓词的概述。也许它会帮助你以 Prolog 的方式思考。

  is_divisor(+Num,+Factor)

  divisors(+Num,-Factors)
  divisors(+Num,+N,-Factors)

  sum(+List,-Total)
  sum(+List,+Sofar,-Total)

  is_perfect(+N)

  perfect(+N,-List)

+ 和 - 实际上并不是参数名称的一部分。它们是关于作者期望实例化的内容的文档线索。(注意)“+Foo”表示您希望 Foo 在调用谓词时具有值。 “-Foo”表示您希望在调用谓词时 Foo 成为一个变量,并在其完成时为其赋予一个值。 (有点像输入和输出,如果这样思考有帮助的话)

每当你看到像 sum/2 和 sum/3 这样的一对谓词时,很可能 sum/2 就像 sum/3 的包装器正在做类似累加器的事情。

我没有费心让它很好地打印出来。您可以直接在 Prolog 命令行中查询它:

?- perfect(100,L).
L = [28, 6] ;
fail.

我发现 Prolog 谓词可能有用的另一件事是,通常有两种。一种是简单地检查某件事是否属实。对于这种谓词,您希望其他一切都失败。这些往往不需要递归。

其他人会想要遍历一个范围(数字或列表)并始终返回结果,即使它是 0 或 []。对于这些类型的谓词,您需要使用递归并考虑您的基本情况。

HTH。

注意:这称为“模式”,您实际上可以指定它们,编译器/解释器将强制执行它们,但我个人只是在文档中使用它们。还尝试查找包含 Prolog 模式信息的页面,但我找不到好的链接。 :(

So I suspect Frank is looking for an answer in Prolog, and yes it does smell rather homeworky...

For fun I decided to write up my answer. It took me about 50 lines.

So here is the outline of what my predicates look like. Maybe it will help get you thinking the Prolog way.

  is_divisor(+Num,+Factor)

  divisors(+Num,-Factors)
  divisors(+Num,+N,-Factors)

  sum(+List,-Total)
  sum(+List,+Sofar,-Total)

  is_perfect(+N)

  perfect(+N,-List)

The + and - are not really part of the parameter names. They are a documentation clue about what the author expects to be instantiated.(NB) "+Foo" means you expect Foo to have a value when the predicate is called. "-Foo" means you expect to Foo to be a variable when the predicate is called, and give it a value by the time it finishes. (kind of like input and output, if it helps to think that way)

Whenever you see a pair of predicates like sum/2 and sum/3, odds are the sum/2 one is like a wrapper to the sum/3 one which is doing something like an accumulator.

I didn't bother to make it print them out nicely. You can just query it directly in the Prolog command line:

?- perfect(100,L).
L = [28, 6] ;
fail.

Another thing that might be helpful, that I find with Prolog predicates, is that there are generally two kinds. One is one that simply checks if something is true. For this kind of predicate, you want everything else to fail. These don't tend to need to be recursive.

Others will want to go through a range (of numbers or a list) and always return a result, even if it is 0 or []. For these types of predicates you need to use recursion and think about your base case.

HTH.

NB: This is called "mode", and you can actually specify them and the compiler/interpreter will enforce them, but I personally just use them in documentation. Also tried to find a page with info about Prolog mode, but I can't find a good link. :(

滥情空心 2024-08-15 02:32:26

我不确定这是否是您想要的,但您总是可以打印出“6, 28”......

I'm not sure if this is what you were looking for, but you could always just print out "6, 28"...

甲如呢乙后呢 2024-08-15 02:32:26

看起来你需要循环直到 n/2,即 n 的 1/2。将数字相除,如果没有余数,则可以将其包含在总数中,一旦用完 n 的 1/2,则检查添加的总数是否 = 您正在测试的数字。

例如:

#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;

int main(void)
{
    int total=0;

    for(int i = 1; i<=100; i++)
    {
        for( int j=1; j<=i/2; j++)
        {
            if (!(i%j))
            {
                total+=j;
            }
        }
        if (i==total)
        {
            cout << i << " is perfect";
        }
        //it works
        total=0;
    }

    return 0;
}

Well looks like you need to loop up until n/2 that is 1/2 of n. Divide the number and if there is no remainder then you can include it in the total, once you have exhausted 1/2 of n then you check if your total added = the number you are testing.

For instance:

#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;

int main(void)
{
    int total=0;

    for(int i = 1; i<=100; i++)
    {
        for( int j=1; j<=i/2; j++)
        {
            if (!(i%j))
            {
                total+=j;
            }
        }
        if (i==total)
        {
            cout << i << " is perfect";
        }
        //it works
        total=0;
    }

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文