| Write LiveCode script named "Find Prime" to find prime numbers up to the number to be specified. |
| Here is a complete LiveCode script named Find Prime that finds all prime numbers up to a number specified by the user. |
Script: Find Prime (LiveCode) |
command FindPrime
put field "Input" into tMax
put "" into field "Output"
if not isNumber(tMax) or tMax < 2 then
answer "Please enter a whole number greater than or equal to 2."
exit FindPrime
end if
repeat with n = 2 to tMax
if isPrime(n) then
put n & return after field "Output"
end if
end repeat
end FindPrime
function isPrime p
if p = 2 then return true
if p mod 2 = 0 then return false
repeat with i = 3 to sqrt(p) step 2
if p mod i = 0 then return false
end repeat
return true
end isPrime
|
How to Use This in a LiveCode Stack
- Create a text field and name it "Input" (this is where the user enters the maximum number).
- Create another field named "Output" (this will display the prime numbers).
- Create a button and in its mouseUp handler, insert this:
|
on mouseUp
FindPrime
end mouseUp
|