Babbage problem: Difference between revisions
Content added Content deleted
m (→{{header|RPL}}) |
|||
Line 3,582: | Line 3,582: | ||
# syntax is postfixed - which makes sense with stack-based operations |
# syntax is postfixed - which makes sense with stack-based operations |
||
# comments cannot be freely inserted within the code |
# comments cannot be freely inserted within the code |
||
Mr Babbage was a great mathematician and, having worked on the task, he |
Mr Babbage was a great mathematician and, having worked on the task, he had to know that the solution s is a multiple of 8, since |
||
s^2 = n.10^6 + 269696 = 64 * (15625.n + 4214) |
s^2 = n.10^6 + 269696 = 64 * (15625.n + 4214) |
||
and that it is greater than the square root of 269696. A simple but efficient algorithm is then to test all multiples of 8 from 512, which is the multiple just below the square root of 269696, up to the solution. In the worst case, the loop would end at 99736.. |
and that it is greater than the square root of 269696. A simple but efficient algorithm is then to test all multiples of 8 from 512, which is the multiple just below the square root of 269696, up to the solution. In the worst case, the loop would end at 99736.. |
||
Line 3,636: | Line 3,636: | ||
|} |
|} |
||
Some words have been especially created to improve Babbage-readability: <code>→STORE</code> stores values in the appropriate variables, <code>IS°NIL</code> compares stack content to zero, <code>°END</code> actually stores stack content into the variable named just before; <code>Mill</code> <code>Store</code> and <code>PRINT</code> do absolutely nothing and are actually one-word comments: |
Some words have been especially created to improve Babbage-readability: <code>→STORE</code> stores values in the appropriate variables, <code>IS°NIL</code> compares stack content to zero, <code>°END</code> actually stores stack content into the variable named just before; <code>Mill</code>, <code>Store</code> and <code>PRINT</code> do absolutely nothing and are actually one-word comments: |
||
≪ 6 2 '''FOR''' n n ROLL SWAP STO -1 '''STEP''' ≫ '→STORE' STO |
≪ 6 2 '''FOR''' n n ROLL SWAP STO -1 '''STEP''' ≫ '→STORE' STO |
||
≪ 0 == ≫ 'IS°NIL' STO |
≪ 0 == ≫ 'IS°NIL' STO |