Translation error (out of memory)

new topic     » topic index » view thread      » older message » newer message

From the book Puzzled Programmers: 15 Mind-Boggling Story Puzzles to Test Your Programming Prowess : Solutions in Basic, Pascal, and C Jun 1987 by Michael Wiesenberg

Find a series of numbers that add up to a specific number. In the book the number was 10000. With the slowest brute force solution it takes 0.00000 seconds. So I increased the number to 1234567890. That takes a lot longer (794.718 seconds).

Here's the program

atom sum, tstart = time() 
 
constant limit = 1_234_567_890 
 
for i = 1 to floor(limit/2) do 
    sum = 0 
    for j = i to floor(limit/2) + 1 do 
        sum += j 
        if sum > limit then 
            exit 
        elsif sum = limit then 
            printf(1, "from %d to %d adds up to %d\n", {i, j, limit}) 
        end if 
    end for 
end for 
printf(1, "%f seconds\n", time() - tstart) 
puts(1, "all done\n") 

I wanted to see how much of a speed increase I would get by compiling so I did
euc -gcc -con sumtest.ex
and ran it again.
I got the following result


from 102880652 to 102880663 adds up to 1234567890
couldn't alloc 16384 bytes

Fatal run-time error:
Your program has run out of memory.
One moment please...

The interpreted run had 4 more solutions after the one shown.
I tried looking at the C code from the translator but I couldn't really make head or tails of it. I don't understand why it would be allocating more memory, especially 16K. I tried rebooting to possibly remedy memory fragmentation that might have caused the problem but that didn't help any (I didn't really think it would). i tried increasing the stack size but it didn't help.
I messed around some with the target number and discovered that if the number is at or below the integer limit (2^30 - 1) then it completes the run. If it is at or above the integer limit it aborts.

Looking at the generated C code I did notice a difference in that there are a lot of binop and NewDouble calls. Is it possible there is a memory leak?

I changed the for-next loops to while loops and it completed.
I'm stumped.

Lonny

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu