Cross adding Unique values

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
newby12
Posts: 33
Joined: Dec 26, 2021 1:57

Cross adding Unique values

Post by newby12 »

I was playing around with numbers..

I managed to write a program that builds two 4 digit numbers with values between 0 an 9..
It cross adds all the digits , and picks out the two 4 digit sets , that equate to all 16 unique values...

Code: Select all


'Program to build two 4 digit numbers that when cross added or cross multiplied results in 16 unique values.

screen 19

dim as string n1
dim as ubyte v1 , v2

dim as string dict1 = ""
dim as string dict2 = ""
dim as string got_it = ""

dim as longint count = 0
dim as ubyte check

goto redo

start:
    
    for b as ubyte = 0 to 2
        for c as ubyte = b to 3
            if dict1[c] < dict1[b] then swap dict1[b] , dict1[c]
            if dict2[c] < dict2[b] then swap dict2[b] , dict2[c]
        next
    next
    
    cls
    print
    print "Added unique values = " ; got_it
    print
    print "Four digits 1 = " ; dict1
    print "Four digits 2 = " ; dict2
    print
    print "Press key for next round.. or press esc to exit"
    sleep
    if inkey = chr( 27 ) then end
    
    print "WORKING!!!"

redo:
    
    count = 0
    got_it = ""
    dict1 = ""
    dict2 = ""
    
    do
        randomize
        v1 = int( rnd * 10 )
        if instr( 1 , dict1 , str( v1 ) ) = 0 then dict1+= str( v1 )
    loop until len( dict1 ) = 4
    
    do
        randomize
        v2 = int( rnd * 10 )
        if instr( 1 , dict2 , str( v2 ) ) = 0 then dict2+= str( v2 )
    loop until len( dict2 ) = 4
    
    check = 0
    for b as ubyte = 1 to 4
        v1 = val( mid( dict1 , b , 1 ) )
        for c as ubyte = 1 to 4
            v2 = val( mid( dict2 , c , 1 ) )
            n1 = right( "00" + str( v1 + v2 ) , 2 )
            if instr( 1 , got_it , n1 ) = 0 then got_it+= n1 + " " : check+= 1
        next
    next
    
    if check <> 16 then goto redo
    
    goto start
        
sleep 
end

Last edited by fxm on Jan 12, 2022 9:38, edited 1 time in total.
Reason: Topic moved to the dedicated forum.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Cross adding Unique values

Post by Munair »

Maybe this should go under Tips and Tricks?
newby12
Posts: 33
Joined: Dec 26, 2021 1:57

Re: Cross adding Unique values

Post by newby12 »

I rewrote it with Hex chrs...

Now it does 1 string of 5 digits and 1 string of 4 digits..
There's 30 cross added unique values. And it picks out 20 of them..

Code: Select all


screen 19

dim as string n1
dim as ubyte v1
dim as ubyte v2

dim as string dict1 = ""
dim as string dict2 = ""
dim as string got_it = ""
    
print
print "WORKING!!!"

goto redo

start:
    
    for b as ubyte = 0 to 3
        for c as ubyte = b to 4
            if dict1[c] < dict1[b] then swap dict1[b] , dict1[c]
        next
    next
    for b as ubyte = 0 to 2
        for c as ubyte = b to 3
            if dict2[c] < dict2[b] then swap dict2[b] , dict2[c]
        next
    next
    
    cls
    print
    print "Cross adeed unique values = " ; got_it
    print
    print "Set 1 = " ; dict1
    print "Set 2 = " ; dict2
    print
    print "Press key for next round.. or press esc to exit"
    sleep
    if inkey = chr( 27 ) then end
    
    print
    print "WORKING!!!"

redo:

    if inkey = chr( 27 ) then end

    got_it = ""
    dict1 = ""
    dict2 = ""
    
    do
        randomize
        v1 = int( rnd * 16 )
        if instr( 1 , dict1 , hex( v1 ) ) = 0 then dict1+= hex( v1 )
    loop until len( dict1 ) = 5
    
    do
        randomize
        v2 = int( rnd * 16 )
        if instr( 1 , dict2 , hex( v2 ) ) = 0 then dict2+= hex( v2 )
    loop until len( dict2 ) = 4
    
    for b as ubyte = 1 to 5
        v1 = val( "&H" + mid( dict1 , b , 1 ) )
        for c as ubyte = 1 to 4
            v2 = val( "&H" + mid( dict2 , c , 1 ) )
            n1 = right( "00" + str( v1 + v2 ) , 2 )
            if instr( 1 , got_it , n1 ) = 0 then 
                got_it+= n1 + " "
            else
                goto redo
            end if
        next
    next
    
    goto start
        
sleep 
end

newby12
Posts: 33
Joined: Dec 26, 2021 1:57

Re: Cross adding Unique values

Post by newby12 »

I got it all automated!

Now you can set the values at the top of the program , and you don't have to alter any of the code.

Code: Select all


'Program to build two numbers , that when cross added , or cross multiplied , results in all unique values.

screen 19

dim as string n1
dim as ubyte v1 , v2

dim as string set1 = ""
dim as string set2 = ""
dim as string got_it = ""

'===================================
'===================================
'PLAY WITH VALUES HERE
'===================================
'===================================
dim as ubyte set1_len = 8
dim as ubyte set2_len = 4

dim as long set1_rnd = 32
dim as long set2_rnd = 16

'set add_mul to zero for cross add , or one for cross multply
dim as ubyte add_mul = 0
'===================================
'===================================

if set1_rnd < set1_len then print "  ERROR!!... set1_rnd has to be equal or greater than set1_len.."  : sleep : end
if set2_rnd < set2_len then print "  ERROR!!... set2_rnd has to be equal or greater than set2_len.."  : sleep : end

print    
print "WORKING!!!"

goto redo

start:
    
    for b as ubyte = 0 to set1_len - 2
        for c as ubyte = b + 1 to set1_len - 1
            if set1[c] < set1[b] then swap set1[b] , set1[c]
        next
    next
    
    for b as ubyte = 0 to set2_len - 2
        for c as ubyte = b + 1 to set2_len - 1
            if set2[c] < set2[b] then swap set2[b] , set2[c]
        next
    next
    
    cls
    print
    if add_mul = 0 then print "Cross added unique values = " ; got_it
    if add_mul = 1 then print "Cross multiplied unique values = " ; got_it
    print
    print "Set 1 = " ;
    for a as ubyte = 1 to len( set1 )
        print right( "000" + str( set1[a-1] ) , 3 ) + " " ;
    next
    print
    print "Set 2 = " ;
    for a as ubyte = 1 to len( set2 )
        print right( "000" + str( set2[a-1] ) , 3 ) + " " ;
    next
    print
    print
    print "Press key for next round.. or press esc to exit"
    sleep
    if inkey = chr( 27 ) then end
    
    cls
    print
    print "WORKING!!!"

redo:
    
    if inkey = chr( 27 ) then end
    
    got_it = ""
    set1 = ""
    set2 = ""
    
    randomize
    
    do
        v1 = int( rnd * set1_rnd )
        if instr( 1 , set1 , chr( v1 ) ) = 0 then set1+= chr( v1 )
    loop until len( set1 ) = set1_len
    
    do
        v2 = int( rnd * set2_rnd )
        if instr( 1 , set2 , chr( v2 ) ) = 0 then set2+= chr( v2 )
    loop until len( set2 ) = set2_len
    
    for b as ubyte = 1 to set1_len
        v1 = asc( mid( set1 , b , 1 ) )
        for c as ubyte = 1 to set2_len
            v2 = asc( mid( set2 , c , 1 ) )
            if add_mul = 0 then n1 = right( "00000" + str( v1 + v2 ) , 5 )
            if add_mul = 1 then n1 = right( "00000" + str( v1 * v2 ) , 5 )
            if instr( 1 , got_it , n1 ) = 0 then got_it+= n1 + " " else goto redo
        next
    next
    
    goto start
        
sleep 
end

Post Reply