|
|
Up |
|
|
  |
Author: Robert SpykermanRobert Spykerman Date: May 23, 2008 22:23
Hello all,
I thought I might try and learn some mathematics, maybe try some of
the Euler problems. Several I've noticed involve finding primes, and I
thought I might try to find a fast way of finding square roots to
integers.
My math (and forth!) skills unfortunately leave a lot to be desired so
I am unsure on the best way to implement this. I have to admit I stole
this algorithm from wikipedia:
public static int sqrt(int num) {
int op = num;
int res = 0;
int one = 1 << 14; // The second-to-top bit is set: 1L<<30 for
long
// "one" starts at the highest power of four <= the argument.
while (one > op)
one >>= 2;
|
| Show full article (1.81Kb) |
|
| | 27 Comments |
|
  |
Author: Marcel HendrixMarcel Hendrix Date: May 23, 2008 23:27
Robert Spykerman gmail.com> writes Re: what is the fastest way of finding an integer square root?
> I thought I might try and learn some mathematics, maybe try some of
> the Euler problems. Several I've noticed involve finding primes, and I
> thought I might try to find a fast way of finding square roots to
> integers.
The fastest way to find them is too look them up, or at least to use
memoization.
[..]
> 1 30 lshift constant set1@30
> \ finds the highest power of 4 (n2) less than n1
> \ does not consume n1 which is passed to sqrt
> \ (power4) ( n1 -- n1 n2 )
> : (power4) set1@30 begin 2dup U< while 2 rshift repeat ;
[..]
> ;
The fastest and best way to do it is (check the CLF archives)
: SQRT ( n1 -- n2 ) S>F FSQRT F>S ;
|
| Show full article (1.37Kb) |
|
| | no comments |
|
  |
Author: Robert SpykermanRobert Spykerman Date: May 23, 2008 23:54
On May 24, 4:27 pm, m...@iae.nl (Marcel Hendrix) wrote:
> Robert Spykerman gmail.com> writes Re: what is the fastest way of finding an integer square root?
>> I thought I might try and learn some mathematics, maybe try some of
>> the Euler problems. Several I've noticed involve finding primes, and I
>> thought I might try to find a fast way of finding square roots to
>> integers.
>
> The fastest way to find them is too look them up, or at least to use
> memoization.
I'll keep that in mind if it ever becomes necessary, it's a bit
involved I guess.
> The fastest and best way to do it is (check the CLF archives)
> : SQRT ( n1 -- n2 ) S>F FSQRT F>S ;
Thank you! Didn't think of using the fpu *duh!* That's what I will do.
By the way I found one more algorithm that's quite succinct if one has
no fpu:
\ from Paul E Bennet comp.arch.embedded 4 May 2008
: sqrt -1 TUCK DO 2 + DUP +LOOP 2/ ;
Thanks again
|
| Show full article (0.96Kb) |
| 1 Comment |
|
  |
Author: Robert SpykermanRobert Spykerman Date: May 23, 2008 23:54
On May 24, 4:27 pm, m...@iae.nl (Marcel Hendrix) wrote:
> Robert Spykerman gmail.com> writes Re: what is the fastest way of finding an integer square root?
>> I thought I might try and learn some mathematics, maybe try some of
>> the Euler problems. Several I've noticed involve finding primes, and I
>> thought I might try to find a fast way of finding square roots to
>> integers.
>
> The fastest way to find them is too look them up, or at least to use
> memoization.
I'll keep that in mind if it ever becomes necessary, it's a bit
involved I guess.
> The fastest and best way to do it is (check the CLF archives)
> : SQRT ( n1 -- n2 ) S>F FSQRT F>S ;
Thank you! Didn't think of using the fpu *duh!* That's what I will do.
By the way I found one more algorithm that's quite succinct if one has
no fpu:
\ from Paul E Bennet comp.arch.embedded 4 May 2008
: sqrt -1 TUCK DO 2 + DUP +LOOP 2/ ;
Thanks again
|
| Show full article (0.96Kb) |
| no comments |
|
  |
Author: Marcel HendrixMarcel Hendrix Date: May 24, 2008 01:17
Robert Spykerman gmail.com> writes Re: what is the fastest way of finding an integer square root?
> On May 24, 4:27 pm, m...@iae.nl (Marcel Hendrix) wrote:
>> Robert Spykerman gmail.com> writes Re: what is the fastest way of finding an integer square root?
[..]
>> The fastest way to find them is too look them up, or at least to use
>> memoization.
> I'll keep that in mind if it ever becomes necessary, it's a bit
> involved I guess.
No, it's very simple (for single argument functions). You *will* need
memoization for some of the more difficult highly recursive Euler
problems. These are designed such that naive recursive approaches
fail.
The iForth distribution has several examples of this technique (not
only in the Euler directory). Brad Eckert did discuss it in CLF.
> By the way I found one more algorithm that's quite succinct if one has
> no fpu:
> \ from Paul E Bennet comp.arch.embedded 4 May 2008
> : sqrt -1 TUCK DO 2 + DUP +LOOP 2/ ;
|
| Show full article (1.69Kb) |
| no comments |
|
  |
Author: Albert van der HorstAlbert van der Horst Date: May 24, 2008 10:29
In article w8g2000prd.googlegroups.com>,
Robert Spykerman gmail.com> wrote:
>Hello all,
>
>I thought I might try and learn some mathematics, maybe try some of
>the Euler problems. Several I've noticed involve finding primes, and I
>thought I might try to find a fast way of finding square roots to
>integers.
>
>My math (and forth!) skills unfortunately leave a lot to be desired so
>I am unsure on the best way to implement this. I have to admit I stole
>this algorithm from wikipedia:
>
>public static int sqrt(int num) {
> int op = num;
> int res = 0;
> int one = 1 << 14; // The second-to-top bit is set: 1L<<30 for
>long
>
> // "one" starts at the highest power of four <= the argument. ...
|
| Show full article (2.81Kb) |
| no comments |
|
  |
Author: Jeff M.Jeff M. Date: May 24, 2008 10:50
On May 24, 12:23 am, Robert Spykerman gmail.com>
wrote:
> Hello all,
>
> I thought I might try and learn some mathematics, maybe try some of
> the Euler problems. Several I've noticed involve finding primes, and I
> thought I might try to find a fast way of finding square roots to
> integers.
>
Depending on what you want to use the sqrt for, if you dont mind a
little error, you can consider a common inv-sqrt function used in
games:
|
| Show full article (0.86Kb) |
| no comments |
|
  |
Author: Jeff M.Jeff M. Date: May 24, 2008 11:07
On May 24, 12:50 pm, "Jeff M." gmail.com> wrote:
> On May 24, 12:23 am, Robert Spykerman gmail.com>
> wrote:
>
>> Hello all,
>
>> I thought I might try and learn some mathematics, maybe try some of
>> the Euler problems. Several I've noticed involve finding primes, and I
>> thought I might try to find a fast way of finding square roots to
>> integers.
>
> Depending on what you want to use the sqrt for, if you dont mind a
> little error, you can consider a common inv-sqrt function used in
> games:
>
> float fastsqrt(float val) {
> int tmp = *(int *)&val;
> tmp -= 1<<23; /* Remove IEEE bias from exponent (-2^23) */
> /* tmp is now an appoximation to logbase2(val) */
> tmp = tmp >> 1; /* divide by 2 */ ...
|
| Show full article (1.17Kb) |
| no comments |
|
  |
Author: Brad EckertBrad Eckert Date: May 27, 2008 08:04
On May 23, 10:23 pm, Robert Spykerman gmail.com>
wrote:
> I have to admit I stole
> this algorithm from wikipedia:
>>
This algorithm is often attributed to Dijkstra, which makes for easier
Googling. It doesn't map well to C, though. Here's what I use as a
template for assembly versions:
|
| Show full article (0.82Kb) |
| no comments |
|
  |
|
|
  |
Author: Dennis RufferDennis Ruffer Date: Jun 1, 2008 18:41
Here's another version that I found simple enough for machineForth:
\ isqrt.mf translated by Dennis Ruffer 04 Oct 06
\
\ from James Ulery's Computing Integer Square Roots
\ http://www.azillionmonkeys.com/qed/ulerysqroot.pdf
\ /* C version by Jim Ulery */
\ static unsigned julery_isqrt(unsigned long val) {
\ unsigned long temp, g=0, b = 0x8000, bshft = 15;
\ do {
\ if (val >= (temp = (((g << 1) + b)<
\ g += b;
\ val -= temp;
\ }
\ } while (b >>= 1);
\ return g;
\ }
[defined] TestCase [IF] simUnit: testISqrt-XX-XXX ( -- method )
[defined] min-isqrt 0= [IF] \ we only want one of this
|
| Show full article (3.08Kb) |
| no comments |
|
|
|
|