AppleScript名:最大公約数を求めるv4 |
— Created 2017-05-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc set aRes to getGCDbetweenTwoNums(75, 315) of me –> 15 –2つの数の最大公約数を求める on getGCDbetweenTwoNums(aNum, bNum) set aRes to getPrimeFactor(aNum) of me set bRes to getPrimeFactor(bNum) of me –2つのリストを集合として扱い、積集合を求める set aSet to current application’s NSMutableSet’s setWithArray:aRes set bSet to current application’s NSMutableSet’s setWithArray:bRes aSet’s intersectSet:bSet set cRes to aSet’s allObjects() as list set dRes to multipleListElements(cRes) of me return dRes end getGCDbetweenTwoNums –与えられた数を素因数分解する on getPrimeFactor(aTargNum) copy aTargNum to a set pFactList to {} repeat set pRes to checkPrimeNumber(a) of me if pRes = true then set the end of pFactList to a return pFactList end if set aFactor to checkFactor(a) of me set the end of pFactList to aFactor set a to a div aFactor end repeat end getPrimeFactor –素数チェック on checkPrimeNumber(aNum) if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース) return checkPrimeNumberSub1(aNum) of me else set aPrime to current application’s PrimeNumberHelper’s alloc()’s init() return (aPrime’s isPrime:aNum) as boolean end if end checkPrimeNumber –小さい数の場合の素数検出 on checkPrimeNumberSub1(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i –false end if end repeat return true end checkPrimeNumberSub1 –素因数チェック on checkFactor(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i end if end repeat end checkFactor –リスト内の要素をすべて乗算する on multipleListElements(aList) set aNum to 1 set aRes to 1 repeat with i in aList set aRes to aRes * (aNum * i) end repeat return aRes end multipleListElements |
More from my site
(Visited 26 times, 1 visits today)