推介:| Web Hosting | 外賣系統 | Server colocation | Web Shop System | Makeup course |

查看完整版本: 高手快來!!!VB PERFECT NUMBER(完整數)

bid_real 2007-10-24 20:33

高手快來!!!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

fsnow 2007-10-24 22:58

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?

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

bid_real 2007-10-25 18:52

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
頁: [1]
查看完整版本: 高手快來!!!VB PERFECT NUMBER(完整數)