Product of min and max prime factors: Difference between revisions
Content added Content deleted
m (→{{header| Haskell}}: Added a draft in Haskell) |
m (→{{header|AppleScript}}: Dedicated primeFactors handler. Multiplications moved to task code.) |
||
Line 78: | Line 78: | ||
end isPrime |
end isPrime |
||
on primeFactors(n) |
|||
on productOfMinAndMaxPrimeFactors(n) |
|||
if |
if (isPrime(n)) then return {n} |
||
set output to {} |
set output to {} |
||
set sqrt to n ^ 0.5 |
|||
if ((sqrt = sqrt div 1) and (isPrime(sqrt))) then |
|||
set end of output to sqrt div 1 |
|||
set sqrt to sqrt - 1 |
|||
end if |
|||
repeat with i from (sqrt div 1) to 2 by -1 |
|||
if (n mod i is 0) then |
if (n mod i is 0) then |
||
if (isPrime(i)) then set |
if (isPrime(i)) then set beginning of output to i |
||
if (isPrime(n div i)) then set |
if (isPrime(n div i)) then set end of output to n div i |
||
end if |
end if |
||
end repeat |
end repeat |
||
return |
return output |
||
end primeFactors |
|||
end productOfMinAndMaxPrimeFactors |
|||
on join(lst, delim) |
on join(lst, delim) |
||
Line 99: | Line 104: | ||
end join |
end join |
||
on |
on task() |
||
set output to {""} |
set output to {""} |
||
set thisLine to {} |
set thisLine to {" 1"} |
||
repeat with n from |
repeat with n from 2 to 100 |
||
tell primeFactors(n) to set product to (its end) * (its beginning) |
|||
set |
set end of thisLine to text -6 thru end of (" " & product) |
||
if (n mod 10 is 0) then |
if (n mod 10 is 0) then |
||
set |
set end of output to join(thisLine, "") |
||
set thisLine to {} |
set thisLine to {} |
||
end if |
end if |
||
end repeat |
end repeat |
||
return join(output, linefeed) |
return join(output, linefeed) |
||
end |
end task |
||
task()</syntaxhighlight> |
|||
{{output}} |
{{output}} |