Liberty BASIC Community Forum
« calculation puzzle »

Welcome Guest. Please Login or Register.
Aug 23rd, 2017, 8:02pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: calculation puzzle  (Read 467 times)
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx calculation puzzle
« Thread started on: Dec 14th, 2015, 04:41am »

i fount a puzzle in the newspaper
i didnt want to solve it by hand
so i wrote a program

its the slowest way posible
its verry brute force

whit a permutation function it can be faster

Code:
''dfbf / bgak =    b
''   -      +      +
''hhfh +  ecc = hkfh
''kjkj - ebak = hkfe
for a = 0 to 9
for b = 0 to 9
for c = 0 to 9
for d = 0 to 9
for e = 0 to 9
for f = 0 to 9
for g = 0 to 9
for h = 0 to 9
for j = 0 to 9
for k = 0 to 9
  q$ = str$( a );b;c;d;e;f;g;h;j;k
  if ispermu( q$ ) then
    a$ = str4$( d , g , b , f )
    b$ = str4$( b , g , a , k )
    c$ = str4$( 0 , 0 , 0 , b )
    d$ = str4$( h , h , f , h )
    e$ = str4$( 0 , e , c , c )
    f$ = str4$( h , k , f , h )
    g$ = str4$( k , j , k , j )
    h$ = str4$( e , b , a , k )
    j$ = str4$( h , k , f , e )
    if val( a$ ) / val( b$ ) = val( c$ ) then
    if val( d$ ) + val( e$ ) = val( f$ ) then
    if val( g$ ) - val( h$ ) = val( j$ ) then
      if val( a$ ) - val( d$ ) = val( g$ ) then
      if val( b$ ) + val( e$ ) = val( h$ ) then
      if val( c$ ) + val( f$ ) = val( j$ ) then
        print a$ + " / " + b$ + " = " + c$
        print "   -      +      +"
        print d$ + " + " + e$ + " = " + f$
        print g$ + " - " + h$ + " = " + j$
        end
      end if
      end if
      end if
    end if
    end if
    end if
  end if
next k
next j
next h
next g
next f
next e
next d
next c
next b
next a
end
function str4$( x , y , z , w )
  str4$ = str$( x ) ; y ; z ; w
end function
function ispermu( q$ )
  uit = 1
  for i = 0 to 9
    if instr( "0123456789" _
    , str$( i ) ) = 0 then
      uit = 0
    end if
  next i
  ispermu = uit
end function

 

User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx Re: calculation puzzle
« Reply #1 on: Dec 14th, 2015, 04:45am »

update :
- x/0 error solved
Code:
''dfbf / bgak =    b
''   -      +      +
''hhfh +  ecc = hkfh
''kjkj - ebak = hkfe
for a = 0 to 9
for b = 0 to 9
for c = 0 to 9
for d = 0 to 9
for e = 0 to 9
for f = 0 to 9
for g = 0 to 9
for h = 0 to 9
for j = 0 to 9
for k = 0 to 9
  q$ = str$( a );b;c;d;e;f;g;h;j;k
  if ispermu( q$ ) then
    a$ = str4$( d , g , b , f )
    b$ = str4$( b , g , a , k )
    c$ = str4$( 0 , 0 , 0 , b )
    d$ = str4$( h , h , f , h )
    e$ = str4$( 0 , e , c , c )
    f$ = str4$( h , k , f , h )
    g$ = str4$( k , j , k , j )
    h$ = str4$( e , b , a , k )
    j$ = str4$( h , k , f , e )
    if val( b$ ) <> 0 then
    if val( a$ ) / val( b$ ) = val( c$ ) then
    if val( d$ ) + val( e$ ) = val( f$ ) then
    if val( g$ ) - val( h$ ) = val( j$ ) then
      if val( a$ ) - val( d$ ) = val( g$ ) then
      if val( b$ ) + val( e$ ) = val( h$ ) then
      if val( c$ ) + val( f$ ) = val( j$ ) then
        print a$ + " / " + b$ + " = " + c$
        print "   -      +      +"
        print d$ + " + " + e$ + " = " + f$
        print g$ + " - " + h$ + " = " + j$
        end
      end if
      end if
      end if
    end if
    end if
    end if
    end if
  end if
next k
next j
next h
next g
next f
next e
next d
next c
next b
next a
end
function str4$( x , y , z , w )
  str4$ = str$( x ) ; y ; z ; w
end function
function ispermu( q$ )
  uit = 1
  for i = 0 to 9
    if instr( "0123456789" _
    , str$( i ) ) = 0 then
      uit = 0
    end if
  next i
  ispermu = uit
end function

 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx Re: calculation puzzle
« Reply #2 on: Dec 14th, 2015, 05:06am »

update :
- there was a error in ispermutation
Code:
''dfbf / bgak =    b
''   -      +      +
''hhfh +  ecc = hkfh
''kjkj - ebak = hkfe
for a = 0 to 9
for b = 0 to 9
for c = 0 to 9
for d = 0 to 9
for e = 0 to 9
for f = 0 to 9
for g = 0 to 9
for h = 0 to 9
for j = 0 to 9
for k = 0 to 9
  q$ = str$( a );b;c;d;e;f;g;h;j;k
  if ispermutation( q$ ) then
    print q$
    a$ = str4$( d , g , b , f )
    b$ = str4$( b , g , a , k )
    c$ = str4$( 0 , 0 , 0 , b )
    d$ = str4$( h , h , f , h )
    e$ = str4$( 0 , e , c , c )
    f$ = str4$( h , k , f , h )
    g$ = str4$( k , j , k , j )
    h$ = str4$( e , b , a , k )
    j$ = str4$( h , k , f , e )
    if val( a$ ) / val( b$ ) = val( c$ ) then
    if val( d$ ) + val( e$ ) = val( f$ ) then
    if val( g$ ) - val( h$ ) = val( j$ ) then
      if val( a$ ) - val( d$ ) = val( g$ ) then
      if val( b$ ) + val( e$ ) = val( h$ ) then
      if val( c$ ) + val( f$ ) = val( j$ ) then
        print a$ + " / " + b$ + " = " + c$
        print "   -      +      +"
        print d$ + " + " + e$ + " = " + f$
        print g$ + " - " + h$ + " = " + j$
        end
      end if
      end if
      end if
    end if
    end if
    end if
  end if
next k
next j
next h
next g
next f
next e
next d
next c
next b
next a
end
function str4$( x , y , z , w )
  str4$ = str$( x ) ; y ; z ; w
end function
function ispermutation( q$ )
  uit = 1
  for i = 0 to 9
    if instr( q$ , str$( i ) ) = 0 then
      uit = 0
    end if
  next i
  ispermutation = uit
end function

 

User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
LBcode
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 101
xx Re: calculation puzzle
« Reply #3 on: Dec 14th, 2015, 06:16am »

I ran it but it just froze my LB up.
« Last Edit: Dec 14th, 2015, 09:35am by LBcode » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1681
xx Re: calculation puzzle
« Reply #4 on: Dec 14th, 2015, 08:30am »

It is a number puzzle.
Each number present single digit
You are to find single permutation which makes all conditions ("dfbf / bgak = b" etc) true.
As for now, it tries to check all possible permutations
And it goes checking each possible number
That gives us 10^10 possible variants.
So you just as well could quit waiting wink

But
using some maths (or wits, or both) you can significally decrease that time.
After all, such kinds of puzzle are supposed to be solved by paper and pen...

(I did some guesses on 4 of digits and got answer in some 20 minutes - not exactly sure - while reading Wiki on Guardian Dark/Summining Dark)
« Last Edit: Dec 14th, 2015, 08:31am by tsh73 » User IP Logged

damned Dog in the Manger
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx Re: calculation puzzle
« Reply #5 on: Dec 21st, 2015, 03:36am »

update :
- the newspaper gave the next solution
Quote:
5928 / 2964 = 2
- + +
1181 + 300 = 1481
4747 - 3264 = 1483
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1681
xx Re: calculation puzzle
« Reply #6 on: Dec 21st, 2015, 05:52am »

I've got same result.
Here are observations I made:
Code:
hhfh +  ecc = hkfh
 

that makes 'c' equal to 0
Code:
bgak 
  +
  ecc 
  =
ebak
 

that makes e=b+1
Code:
    b
  +
hkfh
  =
hkfe
 

that says e=b+h, hense h = 1
Also,
Code:
dfbf / bgak =    b 

leaves 'b' only be 1, 2 or 3.

With that, revised code:
Code:
'5928 / 2964 = 0002
'   -      +      +
'1181 + 0300 = 1481
'4747 - 3264 = 1483

''dfbf / bgak =    b
''   -      +      +
''hhfh +  ecc = hkfh
''kjkj - ebak = hkfe
for a = 0 to 9
for b = 1 to 3 '0 to 9
for c = 0 to 0 '0 to 9
for d = 0 to 9
'for e = 2 to 4 '0 to 9
    e=b+1
for f = 0 to 9
for g = 0 to 9
for h = 1 to 1  '0 to 9
for j = 0 to 9
for k = 0 to 9
scan
  q$ = str$( a );b;c;d;e;f;g;h;j;k
  if ispermutation( q$ ) then
    print q$
    a$ = str4$( d , g , b , f )
    b$ = str4$( b , g , a , k )
    c$ = str4$( 0 , 0 , 0 , b )
    d$ = str4$( h , h , f , h )
    e$ = str4$( 0 , e , c , c )
    f$ = str4$( h , k , f , h )
    g$ = str4$( k , j , k , j )
    h$ = str4$( e , b , a , k )
    j$ = str4$( h , k , f , e )
    if val( a$ ) / val( b$ ) = val( c$ ) then
    if val( d$ ) + val( e$ ) = val( f$ ) then
    if val( g$ ) - val( h$ ) = val( j$ ) then
      if val( a$ ) - val( d$ ) = val( g$ ) then
      if val( b$ ) + val( e$ ) = val( h$ ) then
      if val( c$ ) + val( f$ ) = val( j$ ) then
        print a$ + " / " + b$ + " = " + c$
        print "   -      +      +"
        print d$ + " + " + e$ + " = " + f$
        print g$ + " - " + h$ + " = " + j$
        end
      end if
      end if
      end if
    end if
    end if
    end if
  end if
next k
next j
next h
next g
next f
'next e
next d
next c
next b
next a
end
function str4$( x , y , z , w )
  str4$ = str$( x ) ; y ; z ; w
end function
function ispermutation( q$ )
  uit = 1
  for i = 0 to 9
    if instr( q$ , str$( i ) ) = 0 then
      uit = 0
    end if
  next i
  ispermutation = uit
end function
 
User IP Logged

damned Dog in the Manger
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx Re: calculation puzzle
« Reply #7 on: Dec 30th, 2015, 07:16am »

update :
- permutatrion added

it shoot be faster now

i dond know how long it wil take to find the answer

code from rosetta code

Code:
''dfbf / bgak =    b
''   -      +      +
''hhfh +  ecc = hkfh
''kjkj - ebak = hkfe
dim a( 11 )
for i = 0 to 9
  a( i ) = i
next i
  for i = 0 to 2*3*4*5*6*7*8*9*10
    call nextpermutation 10
    a = a( 0 )
    b = a( 1 )
    c = a( 2 )
    d = a( 3 )
    e = a( 4 )
    f = a( 5 )
    g = a( 6 )
    h = a( 7 )
    j = a( 8 )
    k = a( 9 )
    print a;b;c;d;e;f;g;h;j;k
    a$ = str4$( d , g , b , f )
    b$ = str4$( b , g , a , k )
    c$ = str4$( 0 , 0 , 0 , b )
    d$ = str4$( h , h , f , h )
    e$ = str4$( 0 , e , c , c )
    f$ = str4$( h , k , f , h )
    g$ = str4$( k , j , k , j )
    h$ = str4$( e , b , a , k )
    j$ = str4$( h , k , f , e )
    if val( a$ ) / val( b$ ) = val( c$ ) then
    if val( d$ ) + val( e$ ) = val( f$ ) then
    if val( g$ ) - val( h$ ) = val( j$ ) then
      if val( a$ ) - val( d$ ) = val( g$ ) then
      if val( b$ ) + val( e$ ) = val( h$ ) then
      if val( c$ ) + val( f$ ) = val( j$ ) then
        print a$ + " / " + b$ + " = " + c$
        print "   -      +      +"
        print d$ + " + " + e$ + " = " + f$
        print g$ + " - " + h$ + " = " + j$
        end
      end if
      end if
      end if
    end if
    end if
    end if
  next i
end
function str4$( x , y , z , w )
  str4$ = str$( x ) ; y ; z ; w
end function
function ispermutation( q$ )
  uit = 1
  for i = 0 to 9
    if instr( q$ , str$( i ) ) = 0 then
      uit = 0
    end if
  next i
  ispermutation = uit
end function
sub nextpermutation n
  i = n
  do
    i = i - 1
  loop until ( i = 0 ) or ( a( i ) < a( i + 1 ) )
  j = i + 1
  k = n
  while j < k
    'swap a(j),a(k)
    tmp = a( j )
    a( j ) = a( k )
    a( k ) = tmp
    j = j + 1
    k = k - 1
  wend
  if i > 0 then
    j = i + 1
    while a( j ) < a( i )
      j = j + 1
    wend
    'swap a(i),a(j)
    tmp = a( j )
    a( j ) = a( i )
    a( i ) = tmp
  end if
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1681
xx Re: calculation puzzle
« Reply #8 on: Dec 31st, 2015, 04:07am »

I tried - got bored after an hour, it look like it would go for hours

But I spot that you use permutation code wrong.
As it is, it rotates digits in array starting from 1, not from 0.
(I spotted it by seeing two 0's in a permutation)
« Last Edit: Dec 31st, 2015, 04:09am by tsh73 » User IP Logged

damned Dog in the Manger
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5517
xx Re: calculation puzzle
« Reply #9 on: Dec 31st, 2015, 07:30am »

Do we have the correct puzzle? How can the newspaper's solution be correct?

dfbf/bgak=b

solution from paper

5928 / 2964 = 2

That gives two solutions for f, 9 and 8

So is Jack correct

Quote:
I think the first line of the puzzle should be:
dfbf / bghk = b NOT dfbf / bgak = b

The first line of the answer should be:
5828 / 2914 = 2 NOT 5928 / 2864 = 2

This makes the correct permutation:
a=6, b=2, c=0, d=5, e=3, f=8, g=9, h=1, j=7, k=4

I hope this clarifies it a bit, not confuses it more...



User IP Logged

bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 903
xx Re: calculation puzzle
« Reply #10 on: Jan 1st, 2016, 06:56am »


so i made a mistake

i lost the puzzle + anwer

so i cant check it

what is the correct way to use the
permutation code ?

[ its imposeble to know everything : ]
[ if you know everything : ]
[ do you know a question from whitch you dont know the anwer ? ]
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
JackKelly
Guest
xx Re: calculation puzzle
« Reply #11 on: Jan 6th, 2016, 11:44am »

The following program should find all permutations that solve the puzzle, if any exist, in less than 10 minutes or so...

Code:
print "Testing all permutations. Please wait a short while ... ";
n = 10
s$=""
for i = 0 to n-1
    s$=s$;i
next
res$=permutation$("", s$)
print: print "Program Complete"
end

Function permutation$(pre$, post$)
    lgth = Len(post$)
    If lgth < 2 Then
        call testperm pre$+post$
    Else
        For i = 1 To lgth
            tmp$=permutation$(pre$+Mid$(post$,i,1),Left$(post$,i-1)+Right$(post$,lgth-i))
        Next i
    End If
End Function

Sub testperm perm$
    a=val(mid$(perm$,1,1))
    b=val(mid$(perm$,2,1))
    c=val(mid$(perm$,3,1))
    d=val(mid$(perm$,4,1))
    e=val(mid$(perm$,5,1))
    f=val(mid$(perm$,6,1))
    g=val(mid$(perm$,7,1))
    h=val(mid$(perm$,8,1))
    j=val(mid$(perm$,9,1))
    k=val(mid$(perm$,10,1))

    eq2=0
    x2=h*1000+h*100+f*10+h
    y2=e*100+c*10+c
    z2=h*1000+k*100+f*10+h
    if x2+y2=z2 then eq2=-1 else exit sub

    eq3=0
    x3=k*1000+j*100+k*10+j
    y3=e*1000+b*100+a*10+k
    z3=h*1000+k*100+f*10+e
    if x3-y3=z3 then eq3=-1 else exit sub

    eq1=0
    x1=d*1000+f*100+b*10+f
    y1=b*1000+g*100+h*10+k
    if y1<>0 and x1/y1=2 then eq1=-1 else exit sub

    if eq1 and eq2 and eq3 then
        print: print "A correct permutation is:"
        print "a=";a;", b=";b;", c=";c;", d=";d;", e=";e;
        print ", f=";f;", g=";g;", h=";h;", j=";j;", k=";k
    end if
End Sub
 
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls