Hi there,
I need to calculate 131,072 unique binary numbers, where the 1s and 0s are always 17 characters long
For example
10000000000000000
01000000000000000
00100000000000000
The 1s designate the position of an item in a physical code (a bit like a barcode, with a variable number of bars)
This is what I have so far:
script so
property theBigList : {}
property posOnly : {}
end script
repeat with q from 1 to 20000
set theList to {}
set z to q
repeat
if ((z mod 2) is 0) is true then
set theList to 1 & theList
set z to ((z - 2) / 2)
else
set theList to 0 & theList
set z to ((z - 1) / 2)
end if
if z is 0 then
exit repeat
end if
end repeat
set xCount to (count theList)
if xCount is less than 17 then
repeat with i from (xCount + 1) to 17
set end of theList to 0
end repeat
end if
if theList is not {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} then
set listwithTabs to my getdelimitedList(theList, tab, text)
set isUnique to listwithTabs is in so's posOnly
if not isUnique then
copy listwithTabs to end of so's posOnly
copy (q & tab & listwithTabs) as text to end of so's theBigList
end if
end if
end repeat
set finalTocopy to my getdelimitedList(so's theBigList, return, text)
on getdelimitedList(theInput, theDL, myClass)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDL
if myClass is list then
set theList to text items of theInput
else
set theList to text items of theInput as text
end if
set AppleScript's text item delimiters to tid
return theList
end getdelimitedList
The script above is set to 20,000 repeats which takes 84 seconds and gives me 10,000 unique codes. North of 20,000 iterations, the time taken goes right up.
I changed the main repeat loop to 131,072 and the script took 1 hour and 8 minutes and only then produced half as many codes (65536). Before I leave my script running all night, is there a better way to achieve the same thing?
The end result needs to go into a mySQL database, so that is why I have the results tab delimited.
Many thanks for any help you could give
Ian