Maybe I shouldn't have posted code so soon, eh? Give the beginners a good workout.
My first posting wasn't right - said there was no solution. Duh.
Puzzles are fun - unless they take months to solve!
Way back when, there used to be competitions, to produce workable games/programs in 10 lines or less...
Do I sense a resurgence? lol
head [x | x <- [1..], ['0'..'9'] == sort (show (x^2) ++ show (x^3))]
I can't beat bplus's 6 lines, so I'm going to cheat and use Haskell instead:Code: [Select]head [x | x <- [1..], ['0'..'9'] == sort (show (x^2) ++ show (x^3))]
That 6 line solution is very odd. You'd probably fall UP if you jumped out of an airplane.
While super short, it's horrible for processing time, which is what I try to optimize for, esp. lately.
Did you know that 2 nested DO-LOOPs is faster than 2 nested FOR-NEXT loops? Not that I
understand why.
Think you could pull it off with no GOTO?
I think you could go shorter like:
a$ = “ 123456789 123456798...”
label: n = n + 1
IF _INSTR(a$, STR$(n * n) + _TRIM$(STR$(n * n * n))) THEN PRINT n, n^2, n ^3 ELSE GOTO label
Of course a$ would need all the permutations of “123456789“, with a space in front to delimitate them. I’m not at home to test it, but it seems to me that 3 lines would be all that’s needed with the program, with such a solution.
I think you could go shorter like:I think you might have a little bit of trouble posting the several megabytes of source code needed to the forum for that...
a$ = “ 123456789 123456798...”
label: n = n + 1
IF _INSTR(a$, STR$(n * n) + _TRIM$(STR$(n * n * n))) THEN PRINT n, n^2, n ^3 ELSE GOTO label
Of course a$ would need all the permutations of “123456789“, with a space in front to delimitate them. I’m not at home to test it, but it seems to me that 3 lines would be all that’s needed with the program, with such a solution.
?69
I think you might have a little bit of trouble posting the several megabytes of source code needed to the forum for that...
And of course, there's the obvious one-liner:Code: [Select]?69
10! permutations = 720*210*24 = 3,628,800 * (10 digits + 1 space) = 11! digits 39,916,8002 programs needed to find that out.
Can a line that long be compiled?
It also helps highlight one of the glaring problems I tend to notice with "shortest program" contests. Sure, somebody might be able to cram everything onto a single line of code, but their overall character count is much higher than some of the other folks out there... Fewest lines does not necessarily equate smallest program, in my opinion, but one has to play by the rules of any contest (and their loopholes...). ;)This is why code golf competitions are on number of characters, not number of lines.
2 programs needed to find that out.
program 1:
FOR I&= 0 TO 39916800
A$=A$+LTRIM$(STR$(INT(RND*10)))
NEXT I&
_CLIPBOARD A$
END
Program 2
A$ = 'PASTE CLIPBOARD HERE
and wait........
and wait.......
and wait.......
will the IDE ever actually process that? I image someday it will but mine never finished.
well to be honest I got tired of waiting. It might have actually crashed but I just force closed the IDE.
OK byte count it is (to save Steve from hell proving theoretical idea), a re-evaluation then of contenders :)
And I was just starting to test my idea, with this little program:Code: QB64: [Select]
NEXT I&
This gives us a single line with 39MB worth of data stored into a single string, and saves it as "test.bas". Running the little program here takes about half a minute to finish generating our string for us, and saving it to disk... Loading the file it creates into the IDE only takes about 2 seconds on my PC, and compilation into a working EXE only takes about 10 seconds or so.
Amazingly enough -- and I'm not certain how the hell this works -- but the EXE is *smaller* than the source code!!
Interestingly enough, neither QB64, nor the c-compiler we pack with QB64, had any issues whatsoever with the code. It loaded, compiled, and executed just as simple as could be!
3 lines for the program (though incredibly loooooong lines of pre-generated data), is the fewest lines I think I'd call possible with this type of program in QB64. (Of course, since we've decided to go by byte-count for "shortest program" and not "line-count", there's no reason for me to generate that data list and prove the concept valid any longer, so I'm not going to bother with any more testing than what I've did here.)
Regardless, it's always fun to try and think outside the box, to find a completely different solution than everyone else's. I think my concept definitely does that, even if it does require a 39MB, 3-line program... ;D
When I copied the bplus six liner it was 201 bytes instead of 199, not too sure why.
Anyway, if we do away with insisting on proper formatting and take a few other tricks, you can get it down to 142 characters:
I also better explicitly state that the QB64 IDE v 1.4 stable sets the bytes and judges the syntax.
Here is the program that wrote the 3 lines, no human would want to!Code: QB64: [Select]
'from "Permutations wo recursion" translate from SmallBasic to QB64 2019-03-31 'from "Permutations" translation from: PowerBASIC, tsh copy from Liberty link 2017-02-04 loadPermsValues results$() 'display result$ 'IF i MOD 30 = 29 THEN ' PRINT "press any to continue..." ' SLEEP ' CLS 'END IF PRINT #1, "IF INSTR(a$, LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1" CLOSE #1 ValuesData: 'this reads data from ValuesData line and translates Permutations to those values 'load values array one way or another? read data RESTORE ValuesData READ r$ done = 1 n = n + 1 values(n) = r$ b$ = "" b$ = b$ + values(a(i)) r(e) = b$ e = e + 1 i = n i = i - 1 j = i + 1 k = n WHILE j < k j = j + 1 k = k - 1 j = i + 1 j = j + 1
BTW I was halfway through the first run that I was expecting to take 3+ hours but only takes <20 mins, when I realized we didn't need any permutation that started with 0, that cuts out 1/10th of them? O well...
@SMcNeill
So Einstein your theory worked! :)
I knew it should. I was just too lazy to prove it. So what do I win? Two cookies for my browser? ;D
I already anticipated playing games with spaces and said thisAh, I didn't realise you meant the formatter and indenter as well - I took it to mean it just had to compile in QB64 1.4, and not my local branch which has a _NUMSQUARECUBEALLDIGITS function