寻找 1 到 100 之间的完美数字
如何生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以我怀疑 Frank 正在 Prolog 中寻找答案,是的,它确实闻起来相当
为了好玩,我决定写下我的答案。我花了大约 50 行。
这是我的谓词的概述。也许它会帮助你以 Prolog 的方式思考。
+ 和 - 实际上并不是参数名称的一部分。它们是关于作者期望实例化的内容的文档线索。(注意)“+Foo”表示您希望 Foo 在调用谓词时具有值。 “-Foo”表示您希望在调用谓词时 Foo 成为一个变量,并在其完成时为其赋予一个值。 (有点像输入和输出,如果这样思考有帮助的话)
每当你看到像 sum/2 和 sum/3 这样的一对谓词时,很可能 sum/2 就像 sum/3 的包装器正在做类似累加器的事情。
我没有费心让它很好地打印出来。您可以直接在 Prolog 命令行中查询它:
我发现 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.
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:
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. :(
我不确定这是否是您想要的,但您总是可以打印出“6, 28”......
I'm not sure if this is what you were looking for, but you could always just print out "6, 28"...
看起来你需要循环直到 n/2,即 n 的 1/2。将数字相除,如果没有余数,则可以将其包含在总数中,一旦用完 n 的 1/2,则检查添加的总数是否 = 您正在测试的数字。
例如:
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: