Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56336 > unrolled thread

What words should be included in a minimal number theory package?

Started bylehs <skydda.barnen@gmail.com>
First post2017-03-26 03:13 -0700
Last post2017-03-28 10:55 -0700
Articles 20 on this page of 31 — 10 participants

Back to article view | Back to comp.lang.forth


Contents

  What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 03:13 -0700
    Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-03-26 06:35 -0700
      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 10:14 -0700
        Re: What words should be included in a minimal number theory package? NN <november.nihal@gmail.com> - 2017-03-26 12:50 -0700
          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 17:03 -0700
            Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 14:16 -1000
              Re: What words should be included in a minimal number theory package? hughaguilar96@gmail.com - 2017-03-26 18:00 -0700
              Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 18:13 -0700
                Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 16:18 -1000
                  Re: What words should be included in a minimal number theory package? Paul Rubin <no.email@nospam.invalid> - 2017-03-26 22:14 -0700
                    Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 23:43 -0700
                      Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 21:38 -1000
                      Re: What words should be included in a minimal number theory package? minforth@arcor.de - 2017-03-27 03:30 -0700
                        Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 04:16 -0700
                  Re: What words should be included in a minimal number theory package? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-28 01:54 +0200
                Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-03-27 12:04 -0700
                  Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 12:56 -0700
                    Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-01 06:38 -0700
                      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-01 07:41 -0700
                        Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-17 16:08 -0700
                          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-17 16:34 -0700
                            Re: What words should be included in a minimal number theory package? Julian Fondren <julian.fondren@gmail.com> - 2017-04-17 20:50 -0700
                              Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 00:20 -0700
                            Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-18 04:29 -0700
                          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 01:31 -0700
                Re: What words should be included in a minimal number theory package? mhx@iae.nl - 2017-04-18 01:33 -0700
                  Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 01:44 -0700
    Re: What words should be included in a minimal number theory package? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-28 01:45 +0200
      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 23:33 -0700
        Re: What words should be included in a minimal number theory package? hughaguilar96@gmail.com - 2017-03-28 10:26 -0700
          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-28 10:55 -0700

Page 1 of 2  [1] 2  Next page →


#56336 — What words should be included in a minimal number theory package?

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-26 03:13 -0700
SubjectWhat words should be included in a minimal number theory package?
Message-ID<162e9326-e7e5-4f7b-a253-70b0c4ada914@googlegroups.com>
While developing my BigZ (https://forthmath.blogspot.se/ https://github.com/Lehs/BigZ) I often create integer sequences which I try to find on https://oeis.org/. Now and then I propose a new sequence and have to design a program that generate the sequence. 

However, I always use Forth which probably is very unknown by OEIS users and lacks the most math words that has to be used, as SQRTF ( u -- floor ) and PRIME ( u -- flag ).

(ANS-Forth)
\ prime \ n -- flag
: Gauss_twins \ n -- a(n)
  dup * locals| square | 0
  begin 1+ dup dup * square + prime
     over 2 + dup * square + prime and
  until ;

So far I've solved the problem by state the stack diagram for words not included in the ANS standard, but it would be nice to refer to a minimal standard package and what about:

sqrtf \ u -- floor 
umod \ u q -- u(mod q)
u*mod \ u v q -- uv(mod q)
u**mod \ u v q -- u**v(mod q)
ugcd \ u v -- w
prime \ u -- flag
nextprime \ u -- p

[toc] | [next] | [standalone]


#56342

Fromkrishna.myneni@ccreweb.org
Date2017-03-26 06:35 -0700
Message-ID<08bb27dd-bf67-4999-805a-b3d0601c1040@googlegroups.com>
In reply to#56336
On Sunday, March 26, 2017 at 5:13:43 AM UTC-5, lehs wrote:
> While developing my BigZ (https://forthmath.blogspot.se/ https://github.com/Lehs/BigZ) I often create integer sequences which I try to find on https://oeis.org/. Now and then I propose a new sequence and have to design a program that generate the sequence. 
> 
> However, I always use Forth which probably is very unknown by OEIS users and lacks the most math words that has to be used, as SQRTF ( u -- floor ) and PRIME ( u -- flag ).
...

Why not use the name PRIME? or ?PRIME since it's a query?

Krishna

[toc] | [prev] | [next] | [standalone]


#56344

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-26 10:14 -0700
Message-ID<1cfd4006-770f-4d78-9b9c-eaab08ebf01d@googlegroups.com>
In reply to#56342
Den söndag 26 mars 2017 kl. 15:35:46 UTC+2 skrev krishna...@ccreweb.org:
> On Sunday, March 26, 2017 at 5:13:43 AM UTC-5, lehs wrote:
> > While developing my BigZ (https://forthmath.blogspot.se/ https://github.com/Lehs/BigZ) I often create integer sequences which I try to find on https://oeis.org/. Now and then I propose a new sequence and have to design a program that generate the sequence. 
> > 
> > However, I always use Forth which probably is very unknown by OEIS users and lacks the most math words that has to be used, as SQRTF ( u -- floor ) and PRIME ( u -- flag ).
> ...
> 
> Why not use the name PRIME? or ?PRIME since it's a query?
> 
> Krishna

PRIME? and ISPRIME are perhaps better than PRIME, I'm just used to the latter. I think that ISPRIME is the most common name.

Forth is not oriented for math, but with a few extensions it works perfect. The main problem is that words as * may or may not result in overflow. There is a lot of nice words in ANS-Forth to use to override that problem: UM* UM/MOD M*/ etc, but it's not always obvious how to use those words to solve the problem. One has to make a lot of reinvention all the time.

With words like UMOD, U*MOD U**MOD a lot of routines are within reach for defining more sophisticated primarily tests and factoring (Pollard rho) which make it easy to define a lot of usual number theoretical functions as totients, big omega and Möbius.

[toc] | [prev] | [next] | [standalone]


#56353

FromNN <november.nihal@gmail.com>
Date2017-03-26 12:50 -0700
Message-ID<d9b39781-1f69-436d-b515-6c6785b7a0f2@googlegroups.com>
In reply to#56344
Hi Lehs, 

Most number theory is beyond me but I wondered if you had looked at 

http://mathworld.wolfram.com/NumberTheory.html. 

for inspiration ?

NN

 

[toc] | [prev] | [next] | [standalone]


#56359

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-26 17:03 -0700
Message-ID<25987e3b-afc0-4b5c-854f-2baeed191fe6@googlegroups.com>
In reply to#56353
Thanks for the link! Since there is a lot of number theory beyond my head too, there are functions I will never implement. Most interesting are integer valued functions which also are possible to realize with varied efficiency. Standard Forth isn't perfect for integer functions but can be extended to be good enough for my purpose. Single cell arithmetic is fast but limited because of overflow problems. Words must be carefully defined and should be collected in a libraries ready to be used. If there was an interest those libraries would grow worldwide, but Forth isn't the first choice for number theorists.

My minimal package will be a minimalistic extension of Forth, with words that probably never will be standard Forth, extentions that makes it more easy to define other words.

[toc] | [prev] | [next] | [standalone]


#56361

From"Elizabeth D. Rather" <erather@forth.com>
Date2017-03-26 14:16 -1000
Message-ID<ZNSdnQmO4I9AxEXFnZ2dnUU7-QnNnZ2d@supernews.com>
In reply to#56359
On 3/26/17 2:03 PM, lehs wrote:
> Thanks for the link! Since there is a lot of number theory beyond my head too, there are functions I will never implement. Most interesting are integer valued functions which also are possible to realize with varied efficiency. Standard Forth isn't perfect for integer functions but can be extended to be good enough for my purpose. Single cell arithmetic is fast but limited because of overflow problems. Words must be carefully defined and should be collected in a libraries ready to be used. If there was an interest those libraries would grow worldwide, but Forth isn't the first choice for number theorists.
>
> My minimal package will be a minimalistic extension of Forth, with words that probably never will be standard Forth, extentions that makes it more easy to define other words.
>

Chuck intended Forth to be wonderful for integer math; it was designed 
that way in days before hardware FP was common. He wrote extensive math 
functions with integer arithmetic, including 16- and 32-bit fixed-point 
fraction arithmetic, trig, least-squares fitting algorithms, and FFTs.

Cheers,
Elizabeth

-- 
Elizabeth R. Conklin
FORTH, Inc.
6080 Center Drive, Suite 600
Los Angeles, CA  90045
USA

[toc] | [prev] | [next] | [standalone]


#56362

Fromhughaguilar96@gmail.com
Date2017-03-26 18:00 -0700
Message-ID<a270860b-6180-4de4-966c-0ceb4826829a@googlegroups.com>
In reply to#56361
On Sunday, March 26, 2017 at 5:16:35 PM UTC-7, Elizabeth D. Rather wrote:
> Chuck intended Forth to be wonderful for integer math; it was designed 
> that way in days before hardware FP was common. He wrote extensive math 
> functions with integer arithmetic, including 16- and 32-bit fixed-point 
> fraction arithmetic, trig, least-squares fitting algorithms, and FFTs.

You are a professional name-dropper --- you don't have any accomplishments of your own --- you have made an entire career out of bragging about how you knew Charles Moore in the 1970s.

I'm glad that Charles Moore wrote extensive math functions with integer arithmetic before you kicked him out of Forth Inc. in 1982 --- good for him! --- but I don't think this gives you the right to say that I'm not a Forth programmer.

> Cheers,
> Elizabeth

Well, it was after 5:00 pm when you posted this --- so you have already begun your drinking --- or did you get a head-start on that at 5:00 am?

Cheers!

[toc] | [prev] | [next] | [standalone]


#56364

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-26 18:13 -0700
Message-ID<97f57e6e-0b9c-4743-b889-6bf8fccd0b57@googlegroups.com>
In reply to#56361
Forth has all what is needed except libraries for functions like:

: isprime \ u -- flag
  dup 5 u< if 2 4 within exit then
  dup 1 and 0= if 0= exit then
  true over sqrtf 3 umax 1+ 3
  do over i umod 0= 
     if 0= leave then 2
  +loop nip ;

: ugcd \ u1 u2 -- u      \ Greatest common divisor
  2dup u< if swap then   \ smallest of a b on top of stack (tos)
  ?dup 0= if exit then   \ return second value if tos is zero
  begin tuck             \ y x y first time b a b
     0 swap um/mod       \ y x 0 y --> y r q
     drop dup 0=         \ y r [r=0]
  until drop ;           \ y

: u*mod \ u1 u2 u3 -- u1*u2(mod u3)
  >r r@ umod swap r@ umod um*
  r> um/mod drop ;

: u**mod \ b a m -- x
  locals| m a b | 
  1                      \ preparation for repeated multiplication
  a log~ 0               \ n 0 n is the number of binary digits in a
  ?do a 1 i lshift and   \ flag the i-th digit of a is 0/1
     if b m u*mod        \ multiply if flag
     then b dup m        \ square b for each i: b b^2 b^4 b^8 ...
     u*mod to b          \ new squared number to b
  loop ;                 \ result of the repeated multiplication

With such libraries Forth would be perfect for integer math. But as it is every user of standard Forth has to reinvent all those words that is necessary building stones for all integer math that might interest mathematicians.

[toc] | [prev] | [next] | [standalone]


#56367

From"Elizabeth D. Rather" <erather@forth.com>
Date2017-03-26 16:18 -1000
Message-ID<M6GdnfHLpdzF60XFnZ2dnUU7-K_NnZ2d@supernews.com>
In reply to#56364
On 3/26/17 3:13 PM, lehs wrote:
> Forth has all what is needed except libraries for functions like:
>
> : isprime \ u -- flag
>   dup 5 u< if 2 4 within exit then
>   dup 1 and 0= if 0= exit then
>   true over sqrtf 3 umax 1+ 3
>   do over i umod 0=
>      if 0= leave then 2
>   +loop nip ;
>
> : ugcd \ u1 u2 -- u      \ Greatest common divisor
>   2dup u< if swap then   \ smallest of a b on top of stack (tos)
>   ?dup 0= if exit then   \ return second value if tos is zero
>   begin tuck             \ y x y first time b a b
>      0 swap um/mod       \ y x 0 y --> y r q
>      drop dup 0=         \ y r [r=0]
>   until drop ;           \ y
>
> : u*mod \ u1 u2 u3 -- u1*u2(mod u3)
>   >r r@ umod swap r@ umod um*
>   r> um/mod drop ;
>
> : u**mod \ b a m -- x
>   locals| m a b |
>   1                      \ preparation for repeated multiplication
>   a log~ 0               \ n 0 n is the number of binary digits in a
>   ?do a 1 i lshift and   \ flag the i-th digit of a is 0/1
>      if b m u*mod        \ multiply if flag
>      then b dup m        \ square b for each i: b b^2 b^4 b^8 ...
>      u*mod to b          \ new squared number to b
>   loop ;                 \ result of the repeated multiplication
>
> With such libraries Forth would be perfect for integer math. But as it is every user of standard Forth has to reinvent all those words that is necessary building stones for all integer math that might interest mathematicians.

I'm not a mathematician myself (databases and UI were my thing when I 
was programming), but I don't recall Chuck or the math guys I worked 
with ever using any of these. What do you do with them?

Cheers,
Elizabeth

-- 
Elizabeth R. Conklin
FORTH, Inc.
6080 Center Drive, Suite 600
Los Angeles, CA  90045
USA

[toc] | [prev] | [next] | [standalone]


#56370

FromPaul Rubin <no.email@nospam.invalid>
Date2017-03-26 22:14 -0700
Message-ID<874lyfuwqi.fsf@nightsong.com>
In reply to#56367
"Elizabeth D. Rather" <erather@forth.com> writes:
> I don't recall Chuck or the math guys I worked with ever using any of
> these. What do you do with them?

They show up in number-theoretic and combinatorial math, and these days
in cryptography.  They wouldn't show up in the traditional physics or
engineering type math that I think was more Chuck's area.

[toc] | [prev] | [next] | [standalone]


#56372

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-26 23:43 -0700
Message-ID<68a22a0b-9137-4654-a5a0-ec3c473b0a9d@googlegroups.com>
In reply to#56370
Den måndag 27 mars 2017 kl. 07:14:46 UTC+2 skrev Paul Rubin:
> "Elizabeth D. Rather" <erather@forth.com> writes:
> > I don't recall Chuck or the math guys I worked with ever using any of
> > these. What do you do with them?
> 
> They show up in number-theoretic and combinatorial math, and these days
> in cryptography.  They wouldn't show up in the traditional physics or
> engineering type math that I think was more Chuck's area.

I guess the single cell arithmetic is mostly for efficient testing of conjectures and similar, but the big integer variants 

bgcd \ b1 b2 -- b3
b*mod \ b1 b2 b3 -- b1*b2(mod b3)
b**mod \ b1 b2 b3 -- b1**b2(mod b3)

seems to be useful in certain applications - if they are fast enough.

Thanks to all the double and mixed precision words in ANS-Forth it's possible to develop fast number theoretical words in Forth, but it's inconvenient to do from scratch and words like this ought to be collected in certain libraries, that doesn't have to contain hundreds of words but just the most basic words in the same style as ANS-Forth extensions.

[toc] | [prev] | [next] | [standalone]


#56374

From"Elizabeth D. Rather" <erather@forth.com>
Date2017-03-26 21:38 -1000
Message-ID<RfidnVrMWqAPXEXFnZ2dnUU7-fHNnZ2d@supernews.com>
In reply to#56372
On 3/26/17 8:43 PM, lehs wrote:
> Den måndag 27 mars 2017 kl. 07:14:46 UTC+2 skrev Paul Rubin:
>> "Elizabeth D. Rather" <erather@forth.com> writes:
>>> I don't recall Chuck or the math guys I worked with ever using any of
>>> these. What do you do with them?
>>
>> They show up in number-theoretic and combinatorial math, and these days
>> in cryptography.  They wouldn't show up in the traditional physics or
>> engineering type math that I think was more Chuck's area.
>
> I guess the single cell arithmetic is mostly for efficient testing of conjectures and similar, but the big integer variants
>
> bgcd \ b1 b2 -- b3
> b*mod \ b1 b2 b3 -- b1*b2(mod b3)
> b**mod \ b1 b2 b3 -- b1**b2(mod b3)
>
> seems to be useful in certain applications - if they are fast enough.
>
> Thanks to all the double and mixed precision words in ANS-Forth it's possible to develop fast number theoretical words in Forth, but it's inconvenient to do from scratch and words like this ought to be collected in certain libraries, that doesn't have to contain hundreds of words but just the most basic words in the same style as ANS-Forth extensions.
>

There is an existing math library that you're welcome to contribute to. 
I'm not sure who's administering it these days, though...

Cheers,
Elizabeth

-- 
Elizabeth R. Conklin
FORTH, Inc.
6080 Center Drive, Suite 600
Los Angeles, CA  90045
USA

[toc] | [prev] | [next] | [standalone]


#56378

Fromminforth@arcor.de
Date2017-03-27 03:30 -0700
Message-ID<7375c34b-8b27-4e34-bdc4-28b97774dbc3@googlegroups.com>
In reply to#56372
Am Montag, 27. März 2017 08:43:24 UTC+2 schrieb lehs:
> Den måndag 27 mars 2017 kl. 07:14:46 UTC+2 skrev Paul Rubin:
> > "Elizabeth D. Rather" <erather@forth.com> writes:
> > > I don't recall Chuck or the math guys I worked with ever using any of
> > > these. What do you do with them?
> > 
> > They show up in number-theoretic and combinatorial math, and these days
> > in cryptography.  They wouldn't show up in the traditional physics or
> > engineering type math that I think was more Chuck's area.
> 
> I guess the single cell arithmetic is mostly for efficient testing of conjectures and similar, but the big integer variants 
> 
> bgcd \ b1 b2 -- b3
> b*mod \ b1 b2 b3 -- b1*b2(mod b3)
> b**mod \ b1 b2 b3 -- b1**b2(mod b3)
> 
> seems to be useful in certain applications - if they are fast enough.
> 
> Thanks to all the double and mixed precision words in ANS-Forth it's possible to develop fast number theoretical words in Forth, but it's inconvenient to do from scratch and words like this ought to be collected in certain libraries, that doesn't have to contain hundreds of words but just the most basic words in the same style as ANS-Forth extensions.

Abd there is the Forth Scientific Library #47
https://www.taygeta.com/fsl/library/big.fth

Depending on your Forth you might better link with the GNU BigNum Library.

Multiplication and particularly division/modulus are tricky and there are many non-trivial optimized algorithms. Karatsuba's one of the more simple ones.

But feel free to reinvent the wheel as long as it is a fun project.  ;-)

[toc] | [prev] | [next] | [standalone]


#56379

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-27 04:16 -0700
Message-ID<1e3ab926-d872-42ef-826f-2ff34363a798@googlegroups.com>
In reply to#56378
Den måndag 27 mars 2017 kl. 12:30:01 UTC+2 skrev minf...@arcor.de:
> Am Montag, 27. März 2017 08:43:24 UTC+2 schrieb lehs:
> > Den måndag 27 mars 2017 kl. 07:14:46 UTC+2 skrev Paul Rubin:
> > > "Elizabeth D. Rather" <erather@forth.com> writes:
> > > > I don't recall Chuck or the math guys I worked with ever using any of
> > > > these. What do you do with them?
> > > 
> > > They show up in number-theoretic and combinatorial math, and these days
> > > in cryptography.  They wouldn't show up in the traditional physics or
> > > engineering type math that I think was more Chuck's area.
> > 
> > I guess the single cell arithmetic is mostly for efficient testing of conjectures and similar, but the big integer variants 
> > 
> > bgcd \ b1 b2 -- b3
> > b*mod \ b1 b2 b3 -- b1*b2(mod b3)
> > b**mod \ b1 b2 b3 -- b1**b2(mod b3)
> > 
> > seems to be useful in certain applications - if they are fast enough.
> > 
> > Thanks to all the double and mixed precision words in ANS-Forth it's possible to develop fast number theoretical words in Forth, but it's inconvenient to do from scratch and words like this ought to be collected in certain libraries, that doesn't have to contain hundreds of words but just the most basic words in the same style as ANS-Forth extensions.
> 
> Abd there is the Forth Scientific Library #47
> https://www.taygeta.com/fsl/library/big.fth
> 
> Depending on your Forth you might better link with the GNU BigNum Library.
> 
> Multiplication and particularly division/modulus are tricky and there are many non-trivial optimized algorithms. Karatsuba's one of the more simple ones.
> 
> But feel free to reinvent the wheel as long as it is a fun project.  ;-)

I already had that fun: https://github.com/Lehs/BIG-INTEGER-ANS-FORTH and I do think that b/mod and b**mod works ok. The advantage of my system is that it use big integer stacks which make programming easy. The disadvantage of my system is that big integer stacks may not be optimal efficient. And that my system only is for unsigned integers.

Also, I have difficulties loading most of the code at https://www.taygeta.com/fsl/library/big.fth 

What I'm doing right now is an experimental minimalist library for single cell arithmetic extensions, perhaps including the most well known functions as RADICAL, TOTIENTS, MÖBIUS, BIGOMEGA, SMALLOMEGA, LEGENDRE etc.

I might organize an experimental library on github, with the ambition being possible to load an all ANS-Forth systems (32/64 bits).

[toc] | [prev] | [next] | [standalone]


#56408

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2017-03-28 01:54 +0200
Message-ID<obc8ne$kg2$1@cherry.spenarnc.xs4all.nl>
In reply to#56367
In article <M6GdnfHLpdzF60XFnZ2dnUU7-K_NnZ2d@supernews.com>,
Elizabeth D. Rather <erather@forth.com> wrote:
>On 3/26/17 3:13 PM, lehs wrote:
>> Forth has all what is needed except libraries for functions like:
>>
>> : isprime \ u -- flag
>>   dup 5 u< if 2 4 within exit then
>>   dup 1 and 0= if 0= exit then
>>   true over sqrtf 3 umax 1+ 3
>>   do over i umod 0=
>>      if 0= leave then 2
>>   +loop nip ;
>>
>> : ugcd \ u1 u2 -- u      \ Greatest common divisor
>>   2dup u< if swap then   \ smallest of a b on top of stack (tos)
>>   ?dup 0= if exit then   \ return second value if tos is zero
>>   begin tuck             \ y x y first time b a b
>>      0 swap um/mod       \ y x 0 y --> y r q
>>      drop dup 0=         \ y r [r=0]
>>   until drop ;           \ y
>>
>> : u*mod \ u1 u2 u3 -- u1*u2(mod u3)
>>   >r r@ umod swap r@ umod um*
>>   r> um/mod drop ;
>>
>> : u**mod \ b a m -- x
>>   locals| m a b |
>>   1                      \ preparation for repeated multiplication
>>   a log~ 0               \ n 0 n is the number of binary digits in a
>>   ?do a 1 i lshift and   \ flag the i-th digit of a is 0/1
>>      if b m u*mod        \ multiply if flag
>>      then b dup m        \ square b for each i: b b^2 b^4 b^8 ...
>>      u*mod to b          \ new squared number to b
>>   loop ;                 \ result of the repeated multiplication
>>
>> With such libraries Forth would be perfect for integer math. But as it is every user of standard Forth has to reinvent all those words that is necessary building stones for all integer math that might interest mathematicians.
>
>I'm not a mathematician myself (databases and UI were my thing when I
>was programming), but I don't recall Chuck or the math guys I worked
>with ever using any of these. What do you do with them?


This screen has modulo arithmetic for all the usuals operations.
It is one of the most heavily used. E.g. the latest euler problem
ask for the solution to be calculated modulo 1,000,000,007
(which is a prime).
So a solution starts with
   1,000,000,007 _m !
and then I can use `` +m '' instead of `` + '' etc.

 0 ( +m -m *m /m %:m **m x^x )                     \ AvdH B6mar23
 1 \ Can be INLINE d.     Suffix n : normalized number.
 2 VARIABLE _m ( Modulo number)   WANT XGCD
 3 : _norm_-m  DUP 0< _m @ AND + ; ( x -- xn ) \ -m<xn<+m
 4 : +m   + _m @ - _norm_-m  ;   ( an bn -- sumn )
 5 : -m   - _norm_-m  ;         ( an bn -- diffn)
 6 : *m   M* _m @ SM/REM DROP ;  ( an bn -- prodn)
 7 : /m    _m @ XGCD DROP _norm_-m  *m ; ( a b -- quotn)
 8 : %:m  S>D _m @ SM/REM DROP _norm_-m  ;  ( a -- an)
 9
 10 \ Both steps: For A B and C: return A B en C.  Invariant A*B^C.
 11 : _reduce_1-  1- >R >R    R@ *m   R> R> ;
 12 : _reduce_2/  2/ >R    DUP *m        R> ;
 13 : **m    1 ROT ROT BEGIN   DUP 1 AND IF   _reduce_1-   THEN
 14     _reduce_2/ DUP 0= UNTIL   2DROP   ;  ( a b -- apowbn )
 15 : x^x    _m @ >R _m !   **m   R> _m ! ; ( a b m -- a^b mod m )  OK


P.S. Division only works if _m contains a prime.

>
>Cheers,
>Elizabeth
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#56398

Fromkrishna.myneni@ccreweb.org
Date2017-03-27 12:04 -0700
Message-ID<bfcd4cd1-388a-439b-b742-5b8549113e22@googlegroups.com>
In reply to#56364
On Sunday, March 26, 2017 at 8:13:17 PM UTC-5, lehs wrote:
> Forth has all what is needed except libraries for functions like:
> 
> : isprime \ u -- flag
...

A good example to demonstrate your library is to write a sample program to illustrate factoring a number into its composite primes. An efficient factoring word would actually be a useful addition to a scientific/numeric library. BTW, you didn't include NEXT-PRIME in your library?

Krishna


[toc] | [prev] | [next] | [standalone]


#56400

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-27 12:56 -0700
Message-ID<8a4a2c32-b4d5-46ba-90d3-05b8e8d9632c@googlegroups.com>
In reply to#56398
Den måndag 27 mars 2017 kl. 21:04:36 UTC+2 skrev krishna...@ccreweb.org:
> On Sunday, March 26, 2017 at 8:13:17 PM UTC-5, lehs wrote:
> > Forth has all what is needed except libraries for functions like:
> > 
> > : isprime \ u -- flag
> ...
> 
> A good example to demonstrate your library is to write a sample program to illustrate factoring a number into its composite primes. An efficient factoring word would actually be a useful addition to a scientific/numeric library. BTW, you didn't include NEXT-PRIME in your library?
> 
> Krishna

I've put it in 
https://github.com/Lehs/ANS-Forth-libraries/blob/master/basicsinglext.4th
and it contain NEXTPRIME and PRIMEFACTORS plus some number theoretical functions as RADICAL TOTIENTS MOBIUS BIGOMEGA SMALLOMEGA LEGENDRE TAU and SIGMA.

I think it will work for all true ANS Forth systems. I use LOCALS| since that is the standard. 

From this point any barefoot mathematician forther could start experimenting in number theory and extend on their own without having to do the cumbersome part direct from the standard.

[toc] | [prev] | [next] | [standalone]


#56537

Fromkrishna.myneni@ccreweb.org
Date2017-04-01 06:38 -0700
Message-ID<c95b4a8c-6895-4b51-ba99-0b7536560af6@googlegroups.com>
In reply to#56400
On Monday, March 27, 2017 at 2:56:27 PM UTC-5, lehs wrote:
> Den måndag 27 mars 2017 kl. 21:04:36 UTC+2 skrev krishna...@ccreweb.org:
> > On Sunday, March 26, 2017 at 8:13:17 PM UTC-5, lehs wrote:
> > > Forth has all what is needed except libraries for functions like:
> > > 
> > > : isprime \ u -- flag
> > ...
> > 
> > A good example to demonstrate your library is to write a sample program to illustrate factoring a number into its composite primes. An efficient factoring word would actually be a useful addition to a scientific/numeric library. BTW, you didn't include NEXT-PRIME in your library?
> > 
> > Krishna
> 
> I've put it in 
> https://github.com/Lehs/ANS-Forth-libraries/blob/master/basicsinglext.4th
> and it contain NEXTPRIME and PRIMEFACTORS plus some number theoretical functions as RADICAL TOTIENTS MOBIUS BIGOMEGA SMALLOMEGA LEGENDRE TAU and SIGMA.
> 
> I think it will work for all true ANS Forth systems. I use LOCALS| since that is the standard. 
> 
> From this point any barefoot mathematician forther could start experimenting in number theory and extend on their own without having to do the cumbersome part direct from the standard.

Thanks for the nice code. I'll try it out.

Krishna

[toc] | [prev] | [next] | [standalone]


#56538

Fromlehs <skydda.barnen@gmail.com>
Date2017-04-01 07:41 -0700
Message-ID<a17a6bd6-8b98-4671-ba13-6e921de50242@googlegroups.com>
In reply to#56537
Den lördag 1 april 2017 kl. 15:38:24 UTC+2 skrev krishna...@ccreweb.org:
> On Monday, March 27, 2017 at 2:56:27 PM UTC-5, lehs wrote:
> > Den måndag 27 mars 2017 kl. 21:04:36 UTC+2 skrev krishna...@ccreweb.org:
> > > On Sunday, March 26, 2017 at 8:13:17 PM UTC-5, lehs wrote:
> > > > Forth has all what is needed except libraries for functions like:
> > > > 
> > > > : isprime \ u -- flag
> > > ...
> > > 
> > > A good example to demonstrate your library is to write a sample program to illustrate factoring a number into its composite primes. An efficient factoring word would actually be a useful addition to a scientific/numeric library. BTW, you didn't include NEXT-PRIME in your library?
> > > 
> > > Krishna
> > 
> > I've put it in 
> > https://github.com/Lehs/ANS-Forth-libraries/blob/master/basicsinglext.4th
> > and it contain NEXTPRIME and PRIMEFACTORS plus some number theoretical functions as RADICAL TOTIENTS MOBIUS BIGOMEGA SMALLOMEGA LEGENDRE TAU and SIGMA.
> > 
> > I think it will work for all true ANS Forth systems. I use LOCALS| since that is the standard. 
> > 
> > From this point any barefoot mathematician forther could start experimenting in number theory and extend on their own without having to do the cumbersome part direct from the standard.
> 
> Thanks for the nice code. I'll try it out.
> 
> Krishna

Thank YOU! I've changed the address: https://github.com/Lehs/ANS-Forth-libraries

[toc] | [prev] | [next] | [standalone]


#57505

Fromkrishna.myneni@ccreweb.org
Date2017-04-17 16:08 -0700
Message-ID<86fe1a42-2185-4db4-9d55-9ebd74fa106f@googlegroups.com>
In reply to#56538
On Saturday, April 1, 2017 at 9:41:51 AM UTC-5, lehs wrote:
...
> 
> Thank YOU! I've changed the address: https://github.com/Lehs/ANS-Forth-libraries

I've tried it now. Although I don't know what all of the words do, here are a few comments:

1) The word NEXTPRIME returns the same number if the argument is prime, e.g.

19 nextprime .
19  ok

2) Consider using the common usage word(s), [DEFINED] and/or [UNDEFINED] , instead of devising your own.

3) Maybe an implementation of a prime counting function would be useful to include, for small upper bound, e.g.

: pcf ( u -- ucount )
  0 swap 0 do i isprime if 1+ then loop ;

That's quick and dirty, and not efficient at all once "u" is on the order of 1 million.

4) Some test code, perhaps in a separate file, would help the user understand if the words are working as intended.

Thanks very much for this!

: show-primes ( u -- )
   0 do i isprime if i 6 .r then i 1+ 10 mod 0= if cr then loop ;

100 show-primes
     2     3     5     7
    11    13    17    19
    23    29
    31    37
    41    43    47
    53    59
    61    67
    71    73    79
    83    89
    97
 ok

Krishna

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.forth


csiph-web