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

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 19dim as string n1dim as ubyte v1 , v2dim as string dict1 = ""dim as string dict2 = ""dim as string got_it = ""dim as longint count = 0dim as ubyte checkgoto redostart:        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: 1109
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

### Re: Cross adding Unique values

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

### Re: Cross adding Unique values

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 19dim as string n1dim as ubyte v1dim as ubyte v2dim as string dict1 = ""dim as string dict2 = ""dim as string got_it = ""    printprint "WORKING!!!"goto redostart:        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`