FlashForth: for PIC and Atmega Wiki
Brought to you by:
oh2aun
\ ************
\
\ Filename: prime_factors.txt
\ FlashForth: 5.0
\ MCU PIC18F6527
\ Application: Eval Board
\
\ Author: Pete Zawasky
\ Created: 06:51 PM 01/05/2020 ppz
\ Last Edit 02:58 PM 01/12/2022 ppz
\
\ ************
\ FlashForth is licensed acording to the GNU General Public License
\ *************
\ This code is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty
\ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
\ ************
\ Prime Factorization using FlashForth.
\ Compare to python code calculating prime factors using recursion.
\
\ #re www.wired.com/story/how-to-make-a-tree-with-fractals/
\ #this is the function
\ def pfact(n):
\ if n>1:
\ i=2
\ while n % i !=0:
\ print(n,",",i,",",n//i,",",n%i)
\ i=i+1
\ print(i)
\ #function calling itself -- recursion
\ pfact(n/i)
\ else:
\ print("Finished")
\
\ #change this number for fun
\ pfact(14)
\ #pfact(1234533)
\ *********
-pfact
marker -pfact
decimal ram
\ variables
variable N
variable I
\ Print prime factors of the integer 16-bit u.
\ Unsigned integers preferred.
\
: pfact ( u -- )
N !
begin
N @ 1 u>
while \ u>1
2 I !
begin
N @ I @ u/mod drop 0 <>
while \ N/I=rem not 0
cr N @ dup u. I @ dup u. u/mod u. u. \ print N I N/I rem
1 I +! \ add 1 to I
repeat
cr I @ u. \ print I
N @ I @ u/ N ! \ u=u/I
repeat
cr ." Finished"
;
ram hex