I will periodically post coding challenges to be solved with LiveCode.

Solving problems is a great way to improve your coding skills.

Vote for the best answer.

please paste **plain text** only.

please add **[code]** at the beginning of your code, and **[/code]** at the end to facilitate syntax colorizing.

if you see that **‘AndyP edited….’** your answer, this will be only for formatting.. I will **not** be changing your answer.

A prime number can only be divisible by 1 and itself with the result being a whole number. Even numbers cannot be prime as they are always divisible by 2.

method:

1. list all numbers between 2 and 700

2. remove all even numbers

3. next remove multiples of all prime numbers up to the square root of the highest number that you are checking for primes (700)

Code:

on mouseUp

put empty into field "Field 1"

put empty into field "Field 2"-- list numbers

repeat with i = 2 to 700

put i & return after field "Field 1"

end repeat

put the number of lines of field "Field 1" into tLines

-- remove even numbers

repeat with j = 3 to tLines

if j mod 2 <> 0 then

put j & return after field "Field 2"

end if

end repeat

-- calculate square root of highest number (700)

put the sqrt of 700 into tSqrt

-- remove all multiples upto the square root

-- stuck !!!end mouseUp

Builds two arrays, indexed by “known to be prime and in the range of interest”, and “known to be not prime”.

on mouseUp

local tInput

put fld "FieldIn" into tInput

if the number of words in tInput <> 2 or \

word 1 of tInput is not a number or \

word 2 of tInput is not a number then

ask "must give 2 numbers"

exit mouseUp

end if

local tAPrime, tANonPrime

repeat with i = 2 to word 2 of tInput

if tANonPrime[i] then next repeat

if i >= word 1 of tInput then put true into tAPrime[i]

repeat with j = i+i to word 2 of tInput step i

put true into tANonPrime[j]

end repeat

end repeat

local tt

put "The number of primes between" && tInput && "is" && the number of lines in the keys of tAPrime & CR into fld "F"

put the keys of tAPrime into tt

sort lines of tt numeric

put "and they are" & CR & tt after fld "F"

end mouseUp

on mouseUp

— make an array with uneven numbers between 21 up to 699

put “” into tCollect

repeat with i = 21 to 700 -2 step 2

put true into tCollect[i]

end repeat— get max number that multiplied by 3 will be below 700

— because all I can do is multiply

put trunc (700/3) into tMaxput 3 into tStartVal

repeat with i = 3 to tMax step 2

repeat with j = tStartVal to tMax step 2

put i * j into tSum

if tSum < 700 then

put false into tCollect[tSum]

else

— we are above 700 –> next round

exit repeat

end if

end repeat

add 2 to tStartVal — took this idea from Alex Tweedly’s code

end repeatput the keys of tCollect into tKeys

repeat for each line aKey in tKeys

if tCollect[aKey] = true then put aKey & return after tCollect2

end repeatdelete last char of tCollect2 — a return

sort tCollect2 ascending numeric

put “there are ” & the number of lines of tCollect2 && “prime numbers” & cr & tCollect2 into field 1end mouseUp