close
標題:

vb do loop until

此文章來自奇摩知識+如有不便請留言告知

發問:

'**輸入一個正整數,求出它的質因數。 ' 例如:24=2x2x2x3,24的質因數就是2,2,2,3。 呢條點做..?

最佳解答:

要做這條題目,分開兩個步驟 1. 先找「開方24」以下的全部質數 (即4.899以下的質數) Prime Array is: 2,3 2. 將24用以上的質數由小至大的來除, 使用的除數一定要能夠除盡, 直至答案得出1為止 Round 1: 24/2 = 12 Round 2: 12/2 = 6 Round 3: 6/2 = 3 Round 4: 3/3 = 1 (Finish) 若果你的被除數不是很大, 我建議你將1~1000的質數全部塞入個程式裡, 儲存成Array, 這樣做, 你的程式就可以分解出1~1000^2 (1~1000000) 內的質因數。 以下是1~1000內的所有質數 Dim primes As Integer() = New Integer() {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997} 如果你真的有需要找質數, 可以參考這個程序 Dim primes As New List(Of Integer) Dim m, n As Integer Dim isDivisible As Boolean 'Add first prime primes.Add(2) For n = 3 To 1000 Step 2 'Every odd number isDivisible = False For m = 0 To primes.Count - 1 'Check divisibility with the prime number If n Mod primes(m) = 0 Then isDivisible = True Exit For End If Next If isDivisible = False Then 'Prime number found primes.Add(n) End If Next

其他解答:FAD2A23AB937987B
arrow
arrow

    omckyyo 發表在 痞客邦 留言(0) 人氣()