## Number Trick

General FreeBASIC programming questions.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Number Trick

I learned a number trick on television today...

To the 9's

Code: Select all

`screen 19'A little trick with numbers.. I learned on a TV science show.'You make a 3 digit number then reverse it , and put the bigger number first.'Then you subtract.'The center value will always equal 9 , and the outer 2 numbers , will always add up to 9do        dim as string n1 = right( "000" + str( int( rnd * 1000 ) ) , 3 )    dim as string n2 = mid( n1 , 3 , 1 ) + mid( n1 , 2 , 1 ) + mid( n1 , 1 , 1 )        if n1 > n2 then swap n1 , n2        dim as string n3 = right( "000" + str( val( n2 ) - val( n1 ) ) , 3 )        print    print n2    print n1    print n3        sleep    loop until inkey = chr( 27 )sleepend `

==============================================
If you have any "Number Tricks" you can post them here....
==============================================
Last edited by albert on Aug 23, 2020 2:11, edited 3 times in total.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

I ran it through the whole number spectrum from 1 to 999..

There's only 9 different values + "000" ( 0 to 9 )

Here it is doing 1 to 999

Code: Select all

`screen 19'A little trick with numbers.. I learned on a TV science show.'You make a 3 digit number then reverse it , and put the bigger number first.'Then you subtract.'The center value will always equal 9 , and the outer 2 numbers , will always add up to 9dim as string dict = ""for a as longint = 1 to 999        dim as string n1 = right( "000" + str( a ) , 3 )    dim as string n2 = mid( n1 , 3 , 1 ) + mid( n1 , 2 , 1 ) + mid( n1 , 1 , 1 )        if n1 > n2 then swap n1 , n2        dim as string n3 = right( "000" + str( val( n2 ) - val( n1 ) ) , 3 )        if instr( 1 , dict , n3 ) = 0 then dict+= n3 + " "        print    print n2    print n1    print n3        'sleepnextprintprint "dict = " ; dictsleepend`
deltarho
Posts: 2611
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Number Trick

We get '9' as the magic number because the decimal system is 0 to 9.

With the octal system, 0 to 7, the magic number is 7.

Eaxample:

412, reverse and subtract

412 -
214
----
176

Binary system: magic number is 1

110 -
011
----
011

Hexadecimal system: magic number is F

Hmmm, still working on that one. albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@deltarho

For hex system you'd need 3 hex digits... ( 256 to 4095 )

The scheme requires 3 digits to work..

Here's Hex
The center value will always equal F , and the outer 2 numbers , will always add up to F

Code: Select all

`screen 19'A little trick with numbers.. I learned on a TV science show.'You make a 3 digit number then reverse it , and put the bigger number first.'Then you subtract.'The center value will always equal 9 , and the outer 2 numbers , will always add up to 9dim as string dict = ""for a as longint = 256 to 4095       dim as string n1 = right( "000" + hex( a ) , 3 )    dim as string n2 = mid( n1 , 3 , 1 ) + mid( n1 , 2 , 1 ) + mid( n1 , 1 , 1 )       if n1 > n2 then swap n1 , n2       dim as string n3 = right( "000" + hex( val( "&H" + n2 ) - val( "&H" + n1 ) ) , 3 )       if instr( 1 , dict , n3 ) = 0 then dict+= n3 + " "       print    print n2    print n1    print n3       'sleepnextprintprint "dict = " ; dictprint val( "&H" + "FFF" )sleepend`

The total number of unique values is F ( 0 to 15 ) 16 possible values..
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@deltarho

It should work also with 3 characters.. ( 0 to 255 )

The center value would always equal 255 , and the outer two numbers should always add up to 255...
And there should be 256 values total ( 0 to 255 values )...
deltarho
Posts: 2611
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Number Trick

albert wrote:It should work also with 3 characters.. ( 0 to 255 )

Yes, it does.

I subtracted two hex numbers and got the wrong answer. Mathematicians are not good with simple arithmetic. albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@deltarho

I'm always thinking , when i'm working with numbers , if it can be used to compress data..

But for 3 chrs there's only 256 total outputs for 24 bits , so you couldn't use it to compress... There's too many repeats...
deltarho
Posts: 2611
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Number Trick

@albert

If you want to continue posting on this board I strongly recommend that you never mention 'compress' again. You have no idea how close you have been getting to a permanent suspension. Trust me on that.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Averager Multiplier

Here's an old one i did back in the "Circles" topic...

Multiply by averaging...

Code: Select all

`screen 19do        dim as longint n1 = int( rnd * 1e7 ) + 1    dim as longint n2 = int( rnd * 1e7 ) + 1        if n2 < n1 then swap n1 , n2        dim as string str1 = str( n1 )    dim as string str2 = str( n2 )        dim as double point_5 = n1 / 2    '===============================================================================    dim as double l_bot = val( str( val( left( str2 , 1 ) ) ) + string( len( str2 ) - 1 ,"0" ) )    dim as double l_avg    dim as double l_top = l_bot * 2        dim as double r_bot = val( str( val( str1 ) * ( val( left( str2 , 1 ) ) ) ) + string( len( str2 ) - 1 , "0" ) )    dim as double r_avg    dim as double r_top = r_bot * 2        do                if l_avg = n2 then exit do                l_avg = ( l_bot + l_top ) / 2        r_avg = ( r_bot + r_top ) / 2                if l_avg = n2 then exit do                if right( str( l_avg ) , 2 ) = ".5" then             l_avg = l_avg + .5            r_avg = r_avg + point_5        end if        if l_avg = n2 then exit do               if l_avg > n2 then                 l_top = l_avg                r_top = r_avg        elseif l_avg < n2 then                 l_bot = l_avg                r_bot = r_avg        end if                loop until l_avg = n2        print    print n1 , n2    print  n1 * n2    print r_avg    sleep    loop until inkey = chr( 27 )sleepend  `
Last edited by albert on Aug 23, 2020 2:25, edited 2 times in total.
deltarho
Posts: 2611
Joined: Jan 02, 2017 0:34
Location: UK

### Number Tricks

@albert

STOP changing the topic - it is very annoying. I've just changed it to 'Number Tricks'. Leave it at that.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

@deltarho

I was thinking that :
With the above "Averager Multiplier" , you could alter it , to do 'Division" by averaging...???
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Tricks

deltarho wrote:@albert

STOP changing the topic - it is very annoying. I've just changed it to 'Number Tricks'. Leave it at that.

You change the topic to whatever, so you can search the "Number Trick" Topic of the board , for your your post later on..
If every post starts with "Number Trick" , then you can't find a particular post you might want to retrieve later on...

If "Number Trick" gets to 1,000 pages , how would you search for a particular post you know you made somewhere??

That's the problem i had with "Cricles" and "Squares" , I couldn't find my posts from all the hundreds and thousands of posts i made...
So far I've posted some 5700 posts to the Site.

===================================================================================
They Locked the "Cricles" topic of the board , on account of my posting about "Jews" , I'm not Anti-Semitic...
Now they've locked the "Squares" topic of the board , on account of my posting about "Communism"
I can't explain the issues or they will lock this "Topic" as well...
===================================================================================

So I'll just stick to code posting and asking coding questions , and leave all my other ideas to myself..
deltarho
Posts: 2611
Joined: Jan 02, 2017 0:34
Location: UK

### Re: Number Trick

@albert

If you want to change the topic then start a new topic. Why on earth did you start talking about communism in the Squares thread? You should have started a new topic in the 'Community Discussion' forum.

You started a new topic with Trigonometry-Patterns and that has worked - HASN'T IT?

So change topic => Start New Topic.

So I'll just stick to code posting and asking coding questions , and leave all my other ideas to myself..

Nobody is suggesting that you leave all your other ideas to yourself. Just make sure that a new idea is given its own topic and put into the appropriate sub-forum. Searching will be a lot easier that way.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

The "To the 9's" number trick
Also works with 2 digits... The output always adds up to 9 or equals "00"

Code: Select all

`screen 19'A little trick with numbers.. I learned on a TV science show.'You make a 2 digit number then reverse it , and put the bigger number first.'Then you subtract.'The output 2 numbers , will always add up to 9 or equal "00"do       dim as string n1 = right( "00" + str( int( rnd * 100 ) ) , 2 )    dim as string n2 = mid( n1 , 2 , 1 ) + mid( n1 , 1 , 1 )       if n1 > n2 then swap n1 , n2       dim as string n3 = right( "00" + str( val( n2 ) - val( n1 ) ) , 2 )       print    print n2    print n1    print n3       sleep   loop until inkey = chr( 27 )sleepend `
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Number Trick

It also works with 4 digits...

The 4 output digits always add up to 18...

Code: Select all

`screen 19do       dim as string n1 = right( "0000" + str( int( rnd * 10000 ) ) , 4 )    dim as string n2 = mid( n1 , 4 , 1 ) + mid( n1 , 3 , 1 ) + mid( n1 , 2 , 1 ) + mid( n1 , 1 , 1 )       if n1 > n2 then swap n1 , n2       dim as string n3 = right( "0000" + str( val( n2 ) - val( n1 ) ) , 4 )       print    print n2    print n1    print n3       sleep   loop until inkey = chr( 27 )sleepend`