Number Trick

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

Re: Number Trick

Postby albert » Sep 06, 2020 22:38

@Dodicat

You got the "Linux" source in your above post..

The "Windows" source has the voice stuff at the top...

Otherwise:
I'm out of paper.... I've filled a 500 page notebook with manipulation formulas over the last two weeks.. 1000 pages..
Got to buy another 500 page notebook...
dodicat
Posts: 6719
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Number Trick

Postby dodicat » Sep 07, 2020 14:04

Hi Albert.
I use a small function speak which call vbs instead of using windows.bi.
I only wanted to get the screen text back, it seems missing in yours, but it used to be OK I remember.
angros47
Posts: 1733
Joined: Jun 21, 2005 19:04

Re: Number Trick

Postby angros47 » Sep 07, 2020 16:05

albert wrote:I'm out of paper.... I've filled a 500 page notebook with manipulation formulas over the last two weeks.. 1000 pages..
Got to buy another 500 page notebook...


What are you trying to do? Are you trying to say, like Fermat, that you have a great formula but not enough space to write it?

My question is: does any of your formulas actually work?
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 07, 2020 18:15

@Dodicat

I remember you did a "Halfer" ( div 2 ) fuinction , a while back..

How would you do a halfer for character strings??

&@W#E$R / 2 = ???
Where the output would also be in characters....
dodicat
Posts: 6719
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Number Trick

Postby dodicat » Sep 07, 2020 22:34

Half of &@W#E$R is ½8C«?»A
(note the ½ at the beginning)
If you keep halfing it will always come back to a number.

Code: Select all

Function halfer(fl As String) As String
    Dim As Ubyte main,carry,temp
    Dim As String sign ,s 
    If Instr(fl,".")=0 Then  s=fl+".0" Else s=fl+"0"
    If Instr(s,"-") Then sign="-":s=Ltrim(s,"-")
    Dim As String ans=s
    For z As Integer=0 To Len(s)-1
        If s[z]<>46 Then
            temp=(s[z]-48+carry)
            main=temp Shr 1
            carry=(temp And 1) Shl 3 +(temp And 1) Shl 1
            ans[z]=main+48
        End If
    Next z
    If Instr(ans,".") Then ans=Rtrim(ans,"0")
    ans=Rtrim(ans,".")
    ans=Ltrim(ans,"0")
    Return sign+ans
End Function

dim as string s="&@W#E$R"
dim as long ctr
do
    ctr+=1
    print ctr;"  "; s
    s=halfer(s)
loop until ctr=90
sleep
       
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 07, 2020 23:12

@Dodicat

Can you make it so it returns a character string???
As you half , it need to output a chr , instead of a number..

I was confused by the halfing...

If you have chr( 255 ) and chr( 255)

You half the first chr , you end up with a 127.5 , and you have to add the point 5 to the next chr.. but that makes it go over 255..
So i couldn't figure it out...

==================================================
It doesn't need a sign or end fraction...
i can mod 2 the last chr , and see if it's 0 or 1 , before the halfer
So you can leave the .5 off the end..
==================================================

I'm working on my "Averager" multiplier..

I also need an adder that works on character strings... so i can add the point 5 string ( half of num2 ) , to the output chr string....
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Re: Number Trick

Postby Stonemonkey » Sep 08, 2020 5:29

@Albert

for divide by 2 you shift right by 1, bit 0 of the high byte gets shifted into bit 7 of the low byte.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 08, 2020 17:44

@Dodicat

Never mind... I played with your code and got it working....

How do you modify it , so it doesn't make a fraction at the end???

Code: Select all


screen 19


Function halfer(fl As String) As String
    Dim As Ubyte main,carry,temp
    Dim As String sign ,s
    If Instr(fl,".")=0 Then  s=fl+".0" Else s=fl+"0"
    If Instr(s,"-") Then sign="-":s=Ltrim(s,"-")
    Dim As String ans=s
    For z As Integer=0 To Len(s)-1
        If s[z]<>46 Then
            temp=(s[z]+carry)
            main=temp Shr 1
            carry=(temp And 1) Shl 3 +(temp And 1) Shl 1
            ans[z]=main
        End If
    Next z
    If Instr(ans,".") Then ans=Rtrim(ans,"0")
    ans=Rtrim(ans,".")
    ans=Ltrim(ans,"0")
    Return sign+ans
End Function

dim as string s = "11111111"
dim as long ctr
do
   
    ctr+=1
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
   
    s = halfer(s)
   
    if mid( s , len( s ) - 1 , 1 ) = "." then s = left( s , len( s ) - 2 )
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop until ctr = 90

sleep
       
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Halfer

Postby albert » Sep 08, 2020 20:02

@Dodicat

I got it working.... Now it's returning the correct output characters...

Code: Select all


'Dodicats Halfer...

screen 19

Function halfer(fl As String) As String
    Dim As Ubyte main , carry , temp
    Dim As String s = fl
    Dim As String ans = s
    For z As Integer = 0 To Len( s ) - 1
        If s[ z ] <> 46 Then
            temp = ( s[ z ] + carry )
            main = temp Shr 1
            carry = ( temp And 1 ) Shl 3 + ( temp And 1 ) Shl 1
            ans[ z ] = main
        End If
    Next z
    Return ans
End Function

dim as string s = "11111111"
dim as long ctr
do
   
    ctr+=1
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
   
    s = halfer(s)
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop until ctr = 90

sleep
       
dodicat
Posts: 6719
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Number Trick

Postby dodicat » Sep 08, 2020 21:36

I see what you mean now, I wasn't sure before.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 08, 2020 22:11

@Dodicat

Now i'm back to what i couldn't figure out... How do you handle a string of 255's ???

Since the carry = 5 , then you can't go above 250...

How do you get it to return correct on strings of chr( 255 ) ???

Code: Select all


'Dodicats Halfer...

screen 19


Function halfer(fl As String) As String
    Dim As ubyte main , carry , temp
    Dim As String s = fl
    Dim As String ans = s
    For z As Integer = 0 To Len( s ) - 1
        temp = ( s[ z ] + carry )
        main = temp Shr 1
        carry = ( temp And 1 ) Shl 3 + ( temp And 1 ) Shl 1
        ans[ z ] = main
    Next z
    Return ans
End Function

dim as string s = string( 8 , chr( 255 ) )
dim as long ctr
do
   
    ctr+= 1
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
   
    s = halfer( s )
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop until ctr = 90

sleep
       


@Dodicat

It seems to work if you make the "temp" var ushort instead of ubyte....

Can you test this to see if it works??

Code: Select all


'Dodicats Halfer...

screen 19


Function halfer(fl As String) As String
    Dim As ubyte main , carry
    dim as ushort temp
    Dim As String s = fl
    Dim As String ans = s
    For z As Integer = 0 To Len( s ) - 1
        temp = ( s[ z ] + carry )
        main = temp Shr 1
        carry = ( temp And 1 ) Shl 3 + ( temp And 1 ) Shl 1
        ans[ z ] = main
    Next z
    Return ans
End Function

dim as string s = string( 8 , chr( 255 ) )
dim as long ctr
do
   
    ctr+= 1
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
   
    s = halfer( s )
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop until ctr = 90

sleep
       
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 09, 2020 17:23

@Dodicat

Can you take this "Halfer" and make a "Doubler" to reverse it ???

Code: Select all


'Dodicats Halfer...

screen 19

Function halfer(fl As String) As String
    Dim As ubyte main , carry
    dim as ushort temp
    Dim As String s = fl
    Dim As String ans = s
    For z As Integer = 0 To Len( s ) - 1
        temp = ( s[ z ] + carry )
        main = temp Shr 1
        carry = ( temp And 1 ) Shl 3 + ( temp And 1 ) Shl 1
        ans[ z ] = main
    Next z
    Return ans
End Function

dim as string s = string( 8 , chr( 255 ) )
dim as long ctr
do
   
    ctr+= 1
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
   
    s = halfer( s )
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop

sleep

jj2007
Posts: 1718
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Number Trick

Postby jj2007 » Sep 09, 2020 17:43

Has this become the "Squares 2.0" thread?
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 09, 2020 18:11

@jj2007

I need the "Halfer" and "Doubler" and an "Adder" for my "Averager Multiplier"

It's a number manipulation formula , in keeping with this "Number Trick" topic...

I discovered a way to multiply..

You take the smaller number and put in on the left ,
then take the higher number and put it on the right , extended with 0's to make it as long as then answer...

Then you keep averaging both sides , and when the left side average equals the left number then the right side average will equal the mul result.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Number Trick

Postby albert » Sep 10, 2020 0:24

@Dodicat

Hello Dodicat...

I modified your Halfer , and got it returning correct chr() outputs...

Now i need a "Doubler" to undo the Halfer...
Can you work your magic and conjure up a Doubler ???

Here's your modified "Halfer"

The point_5 var , holds the 1 or 0 if the end results in .5 or 0

Code: Select all


'Dodicats Halfer...

screen 19

Function halfer(fl As String) As String
    Dim As ubyte main , carry
    dim as ushort temp
    Dim As String s = fl
    Dim As String ans = s
    For z As Integer = 0 To Len( s ) - 1
        temp = ( s[ z ] + carry )
        main = temp Shr 1
        carry = ( temp And 1 ) Shl 3 + ( temp And 1 ) Shl 1
        ans[ z ] = main
    Next z
    Return ans
End Function


dim as string s = string( 8 , chr( 255 ) )
dim as string point_5 = ""
dim as long ctr

do
   
    ctr+= 1
   
    if asc( right( s , 1 ) ) mod 2 = 1 then point_5 += "1" else point_5+= "0"
   
    print
    print ctr ;"  ";
    for a as longint = 1 to len( s )
        print asc( mid( s , a , 1 ) ) ; " " ;
    next
    print
   
    s = halfer( s )
   
    sleep
   
    if inkey = chr( 27 ) then exit do
   
loop

print "Point 5 = " ; point_5

sleep



@everyone
Please don't respond unless you can provide a "Doubler" for the "Halfer"

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests