推介:| 修理頂蓬布 | Mediator course | Hypnosis course | Dream Interpretation | NLP | English course |

發新話題
打印

高手快來!!!VB PERFECT NUMBER(完整數)

高手快來!!!VB PERFECT NUMBER(完整數)

An Integer number is said to be a prefect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3

Factor of 6 is 1, 2, 3, (6 not include)

Write a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.

(Hint: Only have 3 numbers is perfect number between 1 and 1000)

我是初學者...
希望各位高手可以用愚蠢的方法教我
不要那麼高深
THANK YOU

TOP

to accomplish a goal, there's alway a stupid way and a much clever way, here's the stupid way:

as stated in the question, a perfect number is the sum of its factors including 1. you want fo find all perfect numbers between 1 and 1000.

1. firstly, you know this has to be done in a loop from 1 to 1000.
2. for each number between 1 to 1000, find its factors and compare the sum with the value
    (hint: to find one's factors, devide the number by all nubers smaller than it, factors are ones with MOD = 0, if you dont know what MOD is, google it
      e.g. 10:
10/1 MOD 0 (Factor),
10/2 MOD 0 (Factor),
10/3 MOD 1 (nop),
10/4 MOD 2 (nop) ,
10/5 MOD 0 (Factor) ,
10/6 MOD 4 (nop) ,
10/7 MOD 3 (nop) ,
10/8 MOD 2 (nop) ,
10/9 MOD 1 (nop)

therefore factors of 10 are 1, 2, 5, the sum is 8 which does not equal to 10, so 10 is not a perfect number.

makes sense?

[ 本帖最後由 fsnow 於 2007-10-24 23:00 編輯 ]

TOP

For num = 1 To 1000
            For factors = 1 To num
                If num Mod factors = 0 Then
                    sum += factors
                    If sum = num Then
                        Console.WriteLine(num)
                    End If
                End If
            Next
        Next

請問係咪類似咁做?
如果有咩錯, 可唔可以話我知點改?
THANKS

TOP

發新話題


重要聲明:本討論區是以即時上載留言的方式運作,本網站對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。本討論區有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿撰寫粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。


Copyright 1997- Xocat. All Right Reserved.