Basicprogramming(.org) > Code and examples

Multiplication with all decimal digits

(1/3) > >>

Galileo:
Simple and fast.

--- Code: ---// 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
--- End code ---

B+:
Simpler?

--- Code: ---_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

--- End code ---

Galileo:
I recognize that your code is of better quality than mine. Congratulations.

B+:

--- Quote from: Galileo on January 29, 2019, 05:01:50 pm ---I recognize that your code is of better quality than mine. Congratulations.

--- End quote ---

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:
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: ---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

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

--- End code ---