Squares

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

Re: Squares

Post by albert »

@D.J.Peters

It's returning right for 16's

But splits into 2 sets for the in between nums

Code: Select all


screen 19

dim as longint n1 , v1 , v2 , ans
for a as longint = 0 to 255
    
    n1 = a
    
    v1 = n1 mod 8
    v2 = n1 \ 16
    
    ans = v1 + ( 16 * v2 )
    
    print n1 ,  v1 , v2 , ans
    
    if a mod 16 = 0 then sleep
    if inkey = chr(27) then exit for
    
next

sleep
end

D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Squares

Post by D.J.Peters »

again you lost bit 3

Joshy

Code: Select all

screen 19

dim as longint n1 , v1 , v2 , ans
for a as longint = 0 to 255
   
    n1 = a
   
    v1 = n1 mod 16 ' <--- !!! bit 3 was missing !!!
    v2 = n1 \ 16
   
    ans = v1 + ( 16 * v2 )
   
    print n1 ,  v1 , v2 , ans
   
    if a mod 16 = 0 then sleep
    if inkey = chr(27) then exit for
   
next

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

Re: Squares

Post by albert »

@D.J.Peters

How do you isolate bit 3 ?

v1 = n1 mod 8 = 3 bits
v2 = n1 \ 16 = 4 bits

7 bits out compresses....

How do you get the 8th bit??

It might not compress with 8 bits out...
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

@Albert.
It is now long past the time that you should have stopped expecting to stumble upon a herd of Unicorns, while looking at patterns in numbers and formulas.

Most 12 year old school students understand that there is a field of mathematics called algebra. They know that algebra makes it possible to manipulate symbols, and to prove if something is actually true, or even possible. Your denial, or refusal to accept the very existence of algebra, is wasting our time and yours.

Learning algebra is easier than computer programming.
Algebra is an essential skill for computer programmers.

I find your claim that you have an engineering qualification quite surprising. An engineering qualification should be impossible without the most trivial beginners algebra, none of which you ever display. The only sensible explanation is that you received a physical or chemical brain injury that wiped out your original school algebra. But, you have learned to write FB programs since then, so you should be able to re-learn your algebra.

I think you should go away for a while to study beginners math of algebra.
Come back when you can explain symbolically, without numbers, why; a = b * a / b.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Squares

Post by D.J.Peters »

I don't understand your sentence !

if you will isolate any bit from a number you build a mask

v = n1 and &B0000100 read only bit 3 from the number
v = n1 and &B11110111 isolate bit 3 from a number

Joshy
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@D.J.Peters

with

v1 mod 8
v2 \ 16

String+= str( v1 ) + str( v2 )

How do i add the missing bit to the string?
call the missing bit v3
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard

Electronics uses very little math ..
I only took "General Math" in high school , i didn't take algebra or geometry or calculus..

In electronics the most difficult formula just uses a square root.

For repairing electronics , they have testers for just about every component on a circuit board.
you have:

voltage in volts
current in amps
resistance in ohms
inductance in henries
capacitance in farads

inductors hardly ever go bad..
So there's no need for inductance formulas , unless your designing an inductor...Then you look in the book for the formula.

If your designing circuits , then you just look in the books for the formulas, they are all fairly simple formulas..

I went to college back in the 80's , so tubes are all obsolete , except for microwave ovens and TV / Radio transmitters..

I don't have an FCC license , you have to be proficient in Morse code , and i had difficulty learning Morse code...So i didn't get the license..
You have to have an FCC license to repair Radar equipment..
I can repair radar equipment , but I'm not licensed to do it.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Albert wrote:I don't have an FCC license , you have to be proficient in Morse code , and i had difficulty learning Morse code...So i didn't get the license..
All Morse code was dumped from the Ham licence in the USA back in early 2007.
Albert wrote:I only took "General Math" in high school , i didn't take algebra or geometry or calculus..
In electronics the most difficult formula just uses a square root.
That explains why you do not understand algebra, but it does not excuse you from learning algebra now.

You still cannot explain why; a = b * a / b.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@D.J.Peters
@Richard

I figured out how to get the missing bit...

n1 = ??
v1 = n1 mod 8
v2 = n1 \ 16
v3 = (n1 and 8) shr 3

Code: Select all


screen 19

dim as longint v1 , v2 , v3

dim as longint n1 = 256

do 
    
        n1-=1
        
        v1 = n1 mod 8
        v2 = n1 \ 16
        v3 = (n1 and 8) shr 3
                
        print
        print n1 , v1 , v2 , v3
        
        for b as longint = 0 to 255 step 1
            
            if (b  mod 8) = v1  and ( (b \ 16)  ) = v2 and ( (b and 8) shr 3 ) = v3 then print b : exit for
            
        next
        
        if n1 mod 10 = 0 then print "press key for next 10   press esc to exit " : sleep
        
        if inkey = chr(27) then end
        
loop until n1 = 0

sleep
end

D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Squares

Post by D.J.Peters »

Don't waste my time ;-)
At timestamp T where v1 was build

Code: Select all

v1 is the value of bit           2,1,0
At timestamp T where v2 was build:

Code: Select all

v2 is the value of bit 7,8,5,4         (shifted to right)
v1 nor v2 knows nothing about bit 3 was it set or clear at timestamp T !

Again the information of bit 3 at timestamp T are lost in space and time forever !

Joshy
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I figured out how to turn 16 bits into 14 bits...But it's lossey compression.. compresses 90+% after 20 loops.

The output is 0 to 4 higher or 1 less.. The under / over requires 3 bits... so the out would be 17 in that case. no compression..

Code: Select all


screen 19

dim as longint n1 , v1 , v2 , ans
dim as single  v3
do
        
        n1 = int( rnd*65536 )
        
        v1 = int( sqr(sqr(n1)) )
        
        v2 = frac( sqr(sqr(n1)) ) *1000
        
        ans = ( v1 + (v2/1000) ) ^ 4
        
        'outs+=right(string(04,"0")+bin(v1),04)
        'outs+=right(string(10,"0")+bin(v2),10)
        
        print
        print n1 , v1 , v2 ,  ans  : sleep
        
        if inkey = chr(27) then exit do
        
loop

sleep
end

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

Re: Squares

Post by albert »

This one stops at 666 and equals 13 bits.... 3 digits of precision...

Code: Select all


screen 19

dim as longint n1 , v1 , fract , ans
dim as single  v2

dim as longint hi1=0 , hi2=0

n1=-1
do
        
        n1+=1
        
        v1 = int( sqr(sqr(n1)) )  : if v1 > hi1 then hi1 = v1
        
        v2 = int(frac( sqr(sqr(n1)) )  * 1000)  : if v2 > hi2 then hi2 = v2
                
        ans = ( v1 + (v2/1000 ) )  ^ 4
        
        print n1 , v1 , v2 ,  ans
        
        if ans - n1 <> 0  then exit do
        
        if inkey = chr(27) then exit do
        
loop

print "len bin hi v1 = " ;  len(bin(hi1)) ; " bits" , hi1
print "len bin hi v2 = " ;  len(bin(hi2)) ; " bits" , hi2

print "len n1 = " ; len(bin(n1)) ; " bits" , ans

sleep
end

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

Re: Squares

Post by albert »

@Richard

It requires 5 decimal places of precision to get to 65,535... 17 bits + 4 bits comes out to 21 bits..

5 places goes to 291,465..
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Albert wrote:@Richard
It requires 5 decimal places of precision to get to 65,535... 17 bits + 4 bits comes out to 21 bits..
5 places goes to 291,465..
@Albert.
That is not a question.
You are having a psychotic episode. Take your medication.

You have insufficient algebra skills to write software without asking for help from others.
Get your ARRL ham licence now, and study algebra ... before you come back to FB.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

This program tells you how many values have a certain number of bits set..

How many values have 1 bit , 2 bits , 3 bits etc... up to "size = ?"

I was going to try to incorporate it into a compression algorithm..

Code: Select all


screen 19

dim as longint size = 16  ' adjust to desired value of bits.

dim as string array(0 to size)

dim as string bins
for a as longint = 0 to (2^size)-1
    
    bins = right(string(size,"0")+bin(a),size)
    
    dim as byte count=0 
    for b as longint = 0 to len(bins)-1 step 1
        if bins[b] = 49 then count+=1
    next
    
    array(count)+=chr(a)
    
next

for a as longint = lbound(array) to ubound(array)
    print a , len(array(a)) ', array(a)
next

sleep
end

Locked