f



Challenge for Christmas

Given a buffer of increasing unsigned integers beginning at addr1 and ending at addr2-cell. I want a routine 

is_a_product  ( u addr2 addr1 -- flag )

answering the question wether u is a product of some numbers in the field or not. 

Example: if the field contains the integers 2,3,4 then 2,6,9,12 should give true flag while 7,10,21 should give false flag. Unless I'm wrong there is a lot of different strategies, but I can't see an obvious one.
0
lehs
12/24/2016 2:10:15 PM
comp.lang.forth 7147 articles. 0 followers. markrobertwills (871) is leader. Post Follow

3 Replies
1089 Views

Similar Articles

[PageSpeed] 59

Is this what you are trying to solve or have I misunderstood ?


: pr1 ( v -- ) ." [" lcount 0 ?do dup i cells + @ . loop drop ." ]" ;

: isaprod ( n v -- f )
	false -rot
	lcount dup
		0 do dup >r
			0 do
				2dup dup
				i cells + @
				swap
				j cells + @ * = if
					2>r drop true 2r>
				then
			loop r>
		loop
	drop 2drop ;

here 3 , 2 , 3 , 4 , value v1

cr v1 pr1
cr 2  v1 isaprod .
cr 6  v1 isaprod .
cr 9  v1 isaprod .
cr 12 v1 isaprod .
cr 7  v1 isaprod .
cr 10 v1 isaprod .
cr 21 v1 isaprod .

NN
0
november
12/24/2016 10:17:43 PM
On Saturday, December 24, 2016 at 11:17:44 PM UTC+1, novembe...@gmail.com wrote:
> Is this what you are trying to solve or have I misunderstood ?
[..]
> : isaprod ( n v -- f )
[..]
> 		0 do dup >r
> 			0 do
[..]
> 				j cells + @ * = if
[..]
> 			loop r>
> 		loop
> 	drop 2drop ;

One cannot access J because of the ">R" before the inner loop.

This might do what you intend:

ANEW -isp
: pr1 ( v -- ) ." [" @+ 0 ?DO @+ . LOOP DROP ." ]" ; 

: isaprod ( n v -- f ) 
        @+ CELLS BOUNDS DLOCAL range  LOCAL v
        range ?DO
               range ?DO
                          I @ J @ *  v = 
                             IF  
                                TRUE UNLOOP UNLOOP EXIT  
                          ENDIF
              CELL +LOOP 
        CELL +LOOP  FALSE ; 

HERE 3 , 2 , 3 , 4 , =: v1 

CR v1 pr1 
CR 2  v1 isaprod . 
CR 6  v1 isaprod . 
CR 9  v1 isaprod . 
CR 12 v1 isaprod . 
CR 7  v1 isaprod . 
CR 10 v1 isaprod . 
CR 21 v1 isaprod . 

-marcel

0
mhx
12/24/2016 11:37:58 PM
ANEW -isp

: pr1 ( v -- ) ." [" @+ 0 ?DO @+ . LOOP DROP ." ]" ; 

: isaprod ( n v -- f ) 
        @+ CELLS BOUNDS DLOCAL range  LOCAL v
        range ?DO
               range ?DO
                          v I @ J @ * = 
                             IF  
                                TRUE UNLOOP UNLOOP EXIT  
                          ENDIF
              CELL +LOOP 
        CELL +LOOP  FALSE ; 

HERE 3 , 2 , 3 , 4 , =: v1 

CR v1 pr1 
CR 2  v1 isaprod . 
CR 6  v1 isaprod . 
CR 9  v1 isaprod . 
CR 12 v1 isaprod . 
CR 7  v1 isaprod . 
CR 10 v1 isaprod . 
CR 21 v1 isaprod . 

Now try CR v1 1024 . :-)

-marcel
0
mhx
12/24/2016 11:43:58 PM
Reply: