Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Code is getting stuck

AnatolyAnatoly Mod
edited August 2018 in Questions Posts: 831

function setup() --displayMode(FULLSCREEN) SCALE = 100 MAXIMUM = SCALE--100 --parameter.integer("Scale", 3, 100, 20, function(v) SCALE = v end) PERCENT = "0%" parameter.watch("PERCENT") tab ={} for x = 1, MAXIMUM do tab[x] = {} for y = 1, MAXIMUM do tab[x][y] = 0 end end for v = 1, MAXIMUM do tab[v][1] = 1 tab[1][v] = 1 end local n1 = 0 for x = 2,MAXIMUM do for y = 2,MAXIMUM do tab[x][y] = tab[x][y-1] + tab[x-1][y] + tab[x-1][y-1] if x == y + 2 then --print("1/" .. tab[x][y] .. " equals " .. 1/tab[x][y]) n1 = n1 + 1/tab[x][y] end end end --print("Sum: " .. n1) --print(isPrime(5), math.ceil(math.sqrt(5))) --AMOUNT = 1 end function draw() background(0) font("Futura-CondensedMedium") --l = 1 local loading = 0 for x = 1,SCALE do for y = 1,SCALE do loading = loading + 1 --if tab[x][y] < math.maxinteger then -- Why? i = math.floor(800/SCALE) fill(255, 255, 255, 255) if x == y then fill(58, 255, 0, 255) elseif tab[x][y] == 1 then fill(255, 166, 0, 255) elseif math.sqrt(tab[x][y]) == math.floor(math.sqrt(tab[x][y])) then fill(255, 0, 0, 255) --[[elseif tab[x][y]%3 == 0 then fill(85, 85, 85, 255) elseif tab[x][y]%5 == 0 then fill(130, 130, 130, 255)]] elseif isPrime(tab[x][y]) then fill(0, 83, 255, 255) end --[[fontSize(i) f,_ = textSize(tab[x][y] .. "") while f > WIDTH/SCALE do i = i - 1 fontSize(i) f,_ = textSize(tab[x][y] .. "") end text(tab[x][y] .. "", WIDTH*(x-.5)/SCALE, HEIGHT*(y-.5)/SCALE)]] rect((x-1)*WIDTH/SCALE, (y-1)*HEIGHT/SCALE, WIDTH/SCALE, HEIGHT/SCALE) --[[l = l + 1 if AMOUNT > l then AMOUNT = AMOUNT + 1 break break end]] PERCENT = (100*loading/SCALE^2) .. "%" print("Handled " .. loading .. " of " .. SCALE^2 .. " numbers\n" .. PERCENT) end end end function isPrime(n) for x = 2, math.ceil(math.sqrt(n)) do --print(n, x, n%x) if n%x == 0 then return false end end return true end

The last message it prints is:

Handled 1279 of 10000.0 numbers
12.79%

Then it doesn’t print anything, but won’t continue.

Comments

  • dave1707dave1707 Mod
    Posts: 7,677

    You’re doing too much in draw. The frames per second is probably 0. You’re calling isPrime a lot which is slowing draw down. You should determine if a number is prime once in setup and not constantly in draw.

Sign In or Register to comment.