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 ((isPrime(n)) or (n is 1)) then return n * n
if (isPrime(n)) then return {n}
set output to {}
set output to {}
repeat with i from (n ^ 0.5 div 1) to 2 by -1
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 output's beginning to i
if (isPrime(i)) then set beginning of output to i
if (isPrime(n div i)) then set output's end to n div i
if (isPrime(n div i)) then set end of output to n div i
end if
end if
end repeat
end repeat
return (output's beginning) * (output's end)
return output
end primeFactors
end productOfMinAndMaxPrimeFactors


on join(lst, delim)
on join(lst, delim)
Line 99: Line 104:
end join
end join


on demo()
on task()
set output to {""}
set output to {""}
set thisLine to {}
set thisLine to {" 1"}
repeat with n from 1 to 100
repeat with n from 2 to 100
tell primeFactors(n) to set product to (its end) * (its beginning)
set thisLine's end to text -6 thru -1 of (" " & productOfMinAndMaxPrimeFactors(n))
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 output's end to join(thisLine, "")
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 demo
end task


demo()</syntaxhighlight>
task()</syntaxhighlight>


{{output}}
{{output}}