VBA生成質因數的函式

2021-09-24 20:05:49 字數 4317 閱讀 4603

rem 生成指定100內的質數陣列

function primenumber()

dim a_prime as variant

a_prime = primenumberformax(100)

primenumber = a_prime

end function

rem 生成指定範圍內的質數陣列

function primenumberformax(byval maxnum as integer) as integer()

redim a_prime_tmp(maxnum) as integer

dim i_count as integer

a_prime_tmp(i_count) = 2

dim i as integer, j as integer

for i = 2 to maxnum

for j = 2 to int(sqr(i)) + 1

if i mod j = 0 then

exit for

elseif j = int(sqr(i)) + 1 then

i_count = i_count + 1

a_prime_tmp(i_count) = i

end if

next

next

redim a_prime(i_count) as integer

for i = 0 to i_count

a_prime(i) = a_prime_tmp(i)

next

primenumberformax = a_prime

end function

rem "獲取陣列長度"

public function arraylength(byval ary) as integer

on error resume next

arraylength = ubound(ary) - lbound(ary) + 1

end function

rem 判斷陣列為空

public function arrayisnull(arr) as boolean

dim i&

on error resume next

i = ubound(arr)

if err = 0 then arrayisnull = true

end function

rem 判斷是否質數

function isprime(byval num as integer) as boolean

dim a_prime as variant

a_prime = primenumberformax(num + 1)

dim i_len as integer

i_len = arraylength(a_prime)

dim i as integer

dim i_tmp as integer

for i = i_len - 1 to 0 step -1

i_tmp = a_prime(i)

if i_tmp = num then

isprime = true

exit for

end if

if i_tmp < num then

isprime = false

exit for

end if

next

end function

rem "分解質因數,獲取質數陣列"

function primefactor(byval num as integer) as integer()

dim reprimfactor() as integer

'判斷不是乙個質數

if isprime(num) then

dim renull() as integer

redim preserve renull(0 to 0) as integer

primefactor = renull

else

'如果不是,則生成乙個比它小一半的質數陣列,對它進行測試

dim halfnum as integer

halfnum = num + 1

'質數陣列

dim a_prime as variant

a_prime = primenumberformax(halfnum)

'返回陣列

dim primfactor as variant

'陣列索引

dim index as integer

index = 0

'對a_prime遍歷,遍歷要放到遞迴函式中,否則演算法效率不高

'再建立乙個函式對陣列進行遞迴

primfactor = getprimefactor(index, a_prime, num)

'整理返回值,去年為0的值

index = 0

for each primf in primfactor

if primf > 0 then

redim preserve reprimfactor(0 to index) as integer

reprimfactor(index) = primfactor(index)

index = index + 1

end if

next

'返回物件primfactor

primefactor = reprimfactor

end if

end function

rem 建立乙個函式對陣列進行遞迴,獲取質因子陣列

rem 質因子陣列則小到大排列

function getprimefactor(byval index as integer, byval a_prim, byval num as integer) as integer()

'本函式中不新增num本身,以免重複

'定義乙個臨時陣列

dim tmp_arr() as integer

redim preserve tmp_arr(0 to 1) as integer

'定義乙個質數

dim prim as integer

'從陣列中按索引index取出這個質數

prim = a_prim(index)

'取模判斷

if num mod prim = 0 then '能被整除,則求商

'求商quotien

dim i_quot as integer

i_quot = num / prim

tmp_arr(0) = prim

'商不為1

if i_quot > 1 then '這個條件是防止商為0

dim a_tmp as variant

a_tmp = getprimefactor(index, a_prim, i_quot)

dim a_len_new as integer

a_len_new = arraylength(a_tmp)

if a_len_new > 0 then

redim preserve tmp_arr(0 to a_len_new) as integer

dim i as integer

i = 1

for each tmp in a_tmp

if (a_tmp(i - 1) > 0) then

tmp_arr(i) = a_tmp(i - 1)

i = i + 1

end if

next

end if

end if

else '不能被整除,則測試下乙個質數

index = index + 1

prim = a_prim(index)

tmp_arr = getprimefactor(index, a_prim, num)

end if

getprimefactor = tmp_arr

end function

function test()

dim n as integer

n = 999

dim a_test as variant

a_test = primefactor(n)

for each i_n in a_test

debug.print " 質因子: " & i_n

next

end function

函式的遞迴並不合理,當測試到halfnum=num/2時,本應該結束迭代,因為後面的迭代是沒有意義的,但是當在迭代因子num不能被prim整除時,會一直測試模運算,最終出錯,以後再改進

質因數的個數

求正整數n n 1 的質因數的個數。相同的質因數需要重複計算。如120 2 2 2 3 5,共有5個質因數。可能有多組測試資料,每組測試資料的輸入是乙個正整數n,1對於每組資料,輸出n的質因數的個數。12020055注意1不是n的質因數 若n為質數,n是n的質因數。include include c...

質因數的個數

求正整數n n 1 的質因數的個數。相同的質因數需要重複計算。如120 2 2 2 3 5,共有5個質因數。可能有多組測試資料,每組測試資料的輸入是乙個正整數n,1示例1 120 5 pat.cpp 定義控制台應用程式的 include stdafx.h include stdio.h includ...

質因數的個數

求正整數n n 1 的質因數的個數。相同的質因數需要重複計算。如120 2 2 2 3 5,共有5個質因數。輸入描述 可能有多組測試資料,每組測試資料的輸入是乙個正整數n,1 輸出描述 對於每組資料,輸出n的質因數的個數。示例1 1205import math def judge num 判斷是否是...