帮助完美数字 [英] Help with Perfect Numbers

查看:48
本文介绍了帮助完美数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的程序必须在1到1000之间找到完美的数字。

它的工作原理,但是它会显示一些不完美的数字,例如第一个完美的数字是:6 28 496


我的程序显示:6 24 28 496


这是我的代码:

Basically my program has to find perfect numbers in a range 1- 1000

it works , kinda but it displays a few numbers that are not perfect for example the first Perfect numbers are : 6 28 496

my program displays : 6 24 28 496

here is my code:

展开 | 选择 | Wrap | 行号

推荐答案

嗯我不确定你在那做什么,但我刚写了一些代码并在前1000个中得到这些数字:

6

28

496


我认为这是因为你检查内部for循环中的总数而不是外部?
Hmm I''m not sure what you''re doing in there, but i just wrote some quick code and got these numbers in the first 1000:
6
28
496

I think its because you check your totals inside the inner for loop, instead of on the outside?


很多论坛经常会发布警告:我们不是为你做功课!


你需要自己找到问题。但是,这是一种打印出需要分析的debuging信息的方法。这个问题应该变得非常明显。祝好运! :

Many forums will often post the caveat: we are not here to do your homework for you!

You need to find the problem yourself. However, here is a way to print out the debuging info you need to analize. The problem should then become pretty obvious. Good Luck! :

展开 | 选择 | Wrap | 行号


我不得不写一段这样的东西......


素数都是奇数,你不需要搜索所有这些......鉴于定义0,1和2是素数,你可以假设这些。


此外,所有数字都可以被它们的平方根整除...所以我们不需要检查任何高于平方根的除数。


Mod返回分子的余数除以分母。鉴于我们知道所有质数都是奇数,所以给出的是2的mod为0的所有数字都是偶数而不是素数...假设一个数字可以被偶数整除,它也可以被整除2 ,素数只能被奇数整除,所以我们甚至不需要检查那些......


所以:


VB
I had to write something like this a while back...

Prime numbers are all odd and you don''t need to search all of them...given that by definition 0, 1 and 2 are prime numbers, you can assume these.

Also, all numbers are divisible by their square roots...so we don''t need to check on any divisors higher than the square root.

Mod returns the remainder of a numerator divided by a denominator. Given that we know that all primes are odd, it is a given that all numbers where the mod of 2 is 0 are even numbers are not prime... given that if a number is divisible by an even number, it is also divisible 2, prime numbers are only divisible by odd numbers, so we don''t even need to check those...

So:

VB
展开 | 选择 | Wrap | 行号


这篇关于帮助完美数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆