Author Topic: Multiplication with all decimal digits  (Read 3745 times)

Galileo

  • Guest
Multiplication with all decimal digits
« on: January 25, 2019, 07:04:35 PM »
Simple and fast.

Code: [Select]
// Display all multiplications of two numbers such that, between the multiplicants and the result are represented once and only once each digit in the decimal system.
// Developed in Yabasic by Galileo, 01/2019

for a = 1 to 99
for b = 1 to 9999
a$ = str$(a)
b$ = str$(b)
c$ = str$(a * b, "%1.0f")
x$ = a$ + b$
lx = len(x$)
lc = len(c$)
if lx = lc then
x$ = x$ + c$
if haveAllDigits(x$) and a < b print a, " * ", b, " = ", c$
elseif lx > lc and lc = 5 then break
end if
next
next

sub haveAllDigits(a$)
local n(57), i, t

for i = 1 to len(a$)
t = asc(mid$(a$, i, 1))
n(t) = n(t) + 1
next

for i = 48 to 57 // ASCII codes from 0 to 9
if n(i) <> 1 return false // more than one occurrence
next

return true
end sub

B+

  • Guest
Re: Multiplication with all decimal digits
« Reply #1 on: January 25, 2019, 09:46:37 PM »
Simpler?
Code: [Select]
_TITLE "All Digits Multiplication B+ mod of Galileo 2019-01-25"

'// Display all multiplications of two numbers such that,
'   between the multiplicants and the result are represented once and only once each digit in the decimal system.
'// Developed in Yabasic by Galileo, 01/2019

FOR a = 1 TO 99
    FOR b = 1 TO 9999
        IF haveAllDigits(LTRIM$(STR$(a)) + LTRIM$(STR$(b)) + LTRIM$(STR$(a * b))) = -1 AND a < b THEN PRINT a, " * ", b, " = ", a * b
    NEXT
NEXT

FUNCTION haveAllDigits (a$)
    IF LEN(a$) <> 10 THEN EXIT FUNCTION
    allDigits$ = "0123456789"
    FOR i = 1 TO LEN(a$)
        p = INSTR(allDigits$, MID$(a$, i, 1))
        IF p THEN allDigits$ = MID$(allDigits$, 1, p - 1) + MID$(allDigits$, p + 1) ELSE EXIT FUNCTION
    NEXT
    haveAllDigits = -1
END FUNCTION


Galileo

  • Guest
Re: Multiplication with all decimal digits
« Reply #2 on: January 29, 2019, 05:01:50 PM »
I recognize that your code is of better quality than mine. Congratulations.

B+

  • Guest
Re: Multiplication with all decimal digits
« Reply #3 on: January 29, 2019, 07:33:40 PM »
I recognize that your code is of better quality than mine. Congratulations.

Thanks, I just learned that trick of eliminating matches at JB from Rod.

It should work well with letters say testing anagrams, maybe with Bulls and Cows game too.

Galileo

  • Guest
Re: Multiplication with all decimal digits
« Reply #4 on: August 30, 2019, 08:09:07 AM »
iBASIC is an outdated interpreter developed by a spanish programmer. Slow, with many bugs but operational. It is interesting to try to write code that overcomes its limitations and takes advantage of its qualities.

Code: [Select]
window TITLE "All Digits Multiplication Galileo mod of B+ 25/01/2019"

'// Display all multiplications of two numbers such that,
'   between the multiplicants and the result are represented once and only once each digit in the decimal system.
'// Developed in Yabasic by Galileo, 01/2019
' iBASIC conversion of Galileo, 08/2019 (http://ibasic.arredemo.org/download.htm)

FOR a = 1 TO 99
    FOR b = 1 TO 9999
        IF call(haveAllDigits(TRIM$(STR$(a)) + TRIM$(STR$(b)) + TRIM$(STR$(a * b)))) = true AND a < b THEN PRINT a; "*"; b; "="; a * b
    NEXT
NEXT

input chr$(10)+"Press ENTER to exit",a$
end

procedure haveAllDigits (in a$) returns r
    la = LEN(a$)
    IF la <> 10 THEN endproc
    allDigits$ = "0123456789"
    FOR i = 1 TO la
        p = INSTR(allDigits$, a$[i])
        IF p THEN allDigits$[p] = "" ELSE endproc
    NEXT
    r = true
ENDproc
« Last Edit: August 30, 2019, 08:18:01 AM by Galileo »

Aurel

  • Guest
Re: Multiplication with all decimal digits
« Reply #5 on: August 30, 2019, 05:02:28 PM »
well i connot found that iBASIC but i found Ecma55
but it looks that is not the same ,,,ecma use line numbers and this one not
and iBASIC looking more modern?

Galileo

  • Guest

Aurel

  • Guest
Re: Multiplication with all decimal digits
« Reply #7 on: September 05, 2019, 01:03:09 PM »
Thanks Galileo
 :)





Peter

  • Guest
Re: Multiplication with all decimal digits
« Reply #8 on: September 05, 2019, 08:04:30 PM »
Interesting challenge, in BaCon it can be done with delimited strings.

Code: [Select]
FOR x = 1 TO 99
    FOR y = 1 TO 9999
        str$ = EXPLODE$(STR$(x) & STR$(y) & STR$(x*y), 1)
        IF AMOUNT(str$) = 10 AND AMOUNT(UNIQ$(str$)) = 10 THEN PRINT x, "*", y, "=", x*y
    NEXT
NEXT

Output:

Quote
3*5694=17082
3*6819=20457
3*6918=20754
3*8169=24507
3*9168=27504
4*3907=15628
4*7039=28156
4*9127=36508
6*5817=34902
7*3094=21658
7*4093=28651
7*9304=65128
7*9403=65821
27*594=16038
36*495=17820
39*402=15678
45*396=17820
46*715=32890
52*367=19084
54*297=16038
63*927=58401
78*345=26910

Aurel

  • Guest
Re: Multiplication with all decimal digits
« Reply #9 on: September 06, 2019, 11:36:25 AM »
funny commands
EXPLODE()
AMOUNT()

 :)

B+

  • Guest
Re: Multiplication with all decimal digits
« Reply #10 on: September 07, 2019, 06:13:01 PM »
I am running an all digits Twice program, past 2 hours now, anyone want to guess how many of those there are?

B+

  • Guest
Re: Multiplication with all decimal digits
« Reply #11 on: September 08, 2019, 01:22:20 PM »
I killed the program after running it for about 20 hours.

At item #186,110
321 * 4069875 = 1306429875   kinda neat how last 3 digits match.

I was running it back from multiplying  two 5 digit numbers and I realized it would take days to get through double and single digit numbers.

Ha! I originally thought maybe DIM an array to store 500 or so.    :P

B+

  • Guest
Re: Multiplication with all decimal digits
« Reply #12 on: September 10, 2019, 02:47:45 AM »
Just 5 digit * 5 digit = 10 digit
Quote
93471 * 94215 = 8806370265
 93462 * 93576 = 8745800112
 93408 * 93702 = 8752516416
 93402 * 93708 = 8752514616
 93352 * 94006 = 8775648112
 93252 * 95163 = 8874140076
 93237 * 94566 = 8817050142
 93205 * 94603 = 8817472615
 93175 * 94732 = 8826654100
 93157 * 93418 = 8702540626
 93126 * 95145 = 8860473270
 93103 * 95074 = 8851674622
 93072 * 95127 = 8853660144
 93052 * 93514 = 8701664728
 93048 * 94215 = 8766517320
 93046 * 94375 = 8781216250
 93025 * 94234 = 8766117850
 93016 * 95314 = 8865727024
 92853 * 94410 = 8766251730
 92802 * 94353 = 8756147106
 92785 * 93304 = 8657211640
 92772 * 95013 = 8814546036
 92764 * 94180 = 8736513520
 92764 * 95134 = 8825010376
 92730 * 93417 = 8662558410

...
 10567 * 99634 = 1052832478
 10566 * 97443 = 1029582738
 10564 * 98377 = 1039254628
 10557 * 98478 = 1039632246
 10542 * 99258 = 1046377836
 10482 * 97653 = 1023598746
 10479 * 98556 = 1032768324
 10456 * 99223 = 1037475688
 10452 * 99267 = 1037538684
 10435 * 98479 = 1027628365
 10372 * 99748 = 1034586256
 10359 * 98874 = 1024235766  item# 39712
« Last Edit: September 10, 2019, 02:50:31 AM by B+ »