Post by B+ on Feb 20, 2020 4:31:12 GMT
I saw this at LB forum posted under Rosetta Code and things only LB could do:
JB can do it too!
It made me curious about QB64 code:
OMG! QB64 sucks at this!
This
So I post a challenge at QB64 forum and the hero of this story is math or someone clever with it.
William F Barnes posts this beauty:
Well I was impressed It might actually find answer faster in our Basics here too!
Indeed it does!
' http://rosettacode.org/wiki/Babbage_problem
' Charles Babbage, gave this example:
' What is the smallest positive integer whose square ends in the digits 269,696 ?
' He thought the answer might be 99,736, whose square is 9,947,269,696; but he couldn't be certain.
n = 1
do
n = n + 1
loop until right$( str$( n^2), 6) ="269696"
' loop until ( n^2 mod 1000000) = 269696
' cf BBC BASIC answer should be using mod not div- copied by a Python user??
print "Smallest number whose square ends in ...269696 is "; n
print "Its square is "; n^2
JB can do it too!
It made me curious about QB64 code:
_TITLE "Find the number which squared ends in ...269696"
DIM n AS _INTEGER64
DO
n = n + 1
IF (n * n) MOD 269696 = 0 THEN PRINT n, n * n, (n * n) MOD 269696
LOOP UNTIL n = 30000
n = 25264
PRINT "The correct number is:"; n; " squared is"; n * n
OMG! QB64 sucks at this!
This
right$( str$( n^2), 6) ="269696"
Doesn't work because of E notation and the above with MOD doesn't work because it can't do MOD with numbers that high.So I post a challenge at QB64 forum and the hero of this story is math or someone clever with it.
William F Barnes posts this beauty:
Nevermind all that bplus, I got you. Watch:
Quote
Find the lowest number which squared ends in ...269696
So I write:
x^2 = A + 269696 ,
for some unknown x and A, and I also know that A = B * 10^6 for some unknown integer B. So:
x^2 = B * 10^6 + 269696
The task is NOT to scan over all x going way into the thousands, but scan over integers B, which only go into the 100s. Then look for x to be an integer as well. Run this code...
Code: QB64: [Select]
DIM x AS DOUBLE
FOR b = 1 TO 1000
x = SQR(b * 10 ^ 6 + 269696)
IF x = INT(x) THEN EXIT FOR
NEXT
PRINT b
PRINT x
PRINT x ^ 2
... get B = 638 and x = 25264.
And we're done.
25264^2 = 638269696
Quote
Find the lowest number which squared ends in ...269696
So I write:
x^2 = A + 269696 ,
for some unknown x and A, and I also know that A = B * 10^6 for some unknown integer B. So:
x^2 = B * 10^6 + 269696
The task is NOT to scan over all x going way into the thousands, but scan over integers B, which only go into the 100s. Then look for x to be an integer as well. Run this code...
Code: QB64: [Select]
DIM x AS DOUBLE
FOR b = 1 TO 1000
x = SQR(b * 10 ^ 6 + 269696)
IF x = INT(x) THEN EXIT FOR
NEXT
PRINT b
PRINT x
PRINT x ^ 2
... get B = 638 and x = 25264.
And we're done.
25264^2 = 638269696
Well I was impressed It might actually find answer faster in our Basics here too!
Indeed it does!
FOR b = 1 TO 1000
x = SQR(b * 10 ^ 6 + 269696)
IF x = INT(x) THEN EXIT FOR
NEXT
PRINT b
PRINT x
PRINT x ^ 2