Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mathimatical Problem

Status
Not open for further replies.

Tryfon

Programmer
Mar 11, 2007
4
CY
Hi guys
am using an 80x86 assembler mips and i have a little problem.
There is a number(int) "n" and i want to find the number(int) wich when you square it you will get either the number n or the int before

Best regards
Tryfon
 
I'm not sure it makes sense mathematically either. To paraphrase:

There is a number(int), 7 say, and i want to find the number(int) wich when you square it you will get either the number 7 or the int before it (i.e. 6 ).

Well neither 6 or 7 have integer square roots.
 
No strettoman ... let me explain more clearly this time:)

For example there is an int 6 and i need to find to find an int which when you square it you ll get an int less or equal than 6 i.e the integer am looking for in the case of 6 is 2(2x2=4 which 4 holds the argument of <=6)
 
This is a standard text-book question, because all you're looking for is a rounded-down integer square root. Try any basic algorithm text, or web-searching for square root methods.
 
If you are using an Intel 80x86 processor, at least from the 486 onwards, you should be able to find some floating point instructions which will let you store a decimal square root to memory, and then read it back as a (rounded down) integer square root.
 
well, i am triing to learn asm. But If I didn't missunderstand you the basic code is ;



Code:
input "enter number";n%  'get an integer

r = 0 'set the root for zero

do                'loop
r = r + 0.1       'increase root by 0.1
loop until r * r >= n%  'check if the r * r >= n%
                        'if it is, then exit loop

if r * r  > n% then r = r - 0.1  'if the square of 
                                 'square roots 
                                 'bigger than n%
                                 'move 1 step back  

nr% = 0

do
if r < 1 then exit do else nr% = nr% + 1
r = r - 1
loop



print "The square root of ";n% is;r
print "And the nearest square is";nr%;"x";nr%;nr%xnr%


where n%, nr% are integer and r is a single (float)




lets n% = 8

so the root will be something bigger than 2.5 and we want only 2

first step ;

nr% = 1 r = 1.5


second :

nr% = 2 r = 0.5

third :

nr% = 2 r is smaller than 1 so exit


I hope the code works. It is a handmade :D square root program. And I would be pleased if you write the explained asm code...
 
sorry for the wrong answer, so here you are ;

The nearest integer that is square rott of any integer in basic


Code:
Input "Enter An Integer";n%

r% = 0

do
r%=r%+1
loop until r% * r% > n%

r% = r% - 1


the integer r% is what you want...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top