WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Number Conversion

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Find Prime Factor of Integer Value

Keywords:   Find Prime Factor of Integer Value

;version : 1.0
;author  ; Delgove Jean-jacques
;date    : 28 may 2002
;purpose : find prime factor of integer value
#DefineFunction factor(n0)
if( ! IsInt(n0) ) then
	return("")
endif
n = n0
msg=strcat("")
n1=1
if ( n < 2 ) then
	return(n)
endif
if ( n > 2 ) then
	d = 2
	k=0
	while ( (n mod d) == 0 )
      		n = n / d
      		n1=n1*d
      		k=k+1
      	endwhile
	switch k
		case 0
			break
		case 1
			msg=strcat(msg,d," * ")
			break
		case k
			msg=strcat(msg,d,"**",k," * ")
	endswitch
	d=3
	while ( 1 )
		k=0
		while ( ( n mod d) == 0)
     	 		n = n/d
     	 		n1=n1*d
     	 		k=k+1
     	 	endwhile
		switch k
			case 0
				break
			case 1
				msg=strcat(msg,d," * ")
				break
			case k
				msg=strcat(msg,d,"**",k," * ")
		endswitch
		d=d+2     
		if ( d*d > n0 ) then
			break
		endif       
	endwhile
endif                  
if( n == n0)then
	;n0 is prime
	msg=n0
else
	if (n1 != n0) then
		;d1 greatest divisor > sqrt(n0)
		d1=n0/n1
		msg=strcat(msg,d1," * ")
	endif
endif
if  ( strsub(msg,strlen(msg)-2,3) == " * ") then
	msg=strsub(msg,1,strlen(msg)-3)
endif
return(msg)
#EndFunction
exclusive(@ON)
h=FileOpen("prime.txt","WRITE")
aaaa=GetTickCount()
n=200
for i=3 to 10000  ;;;;;;;;;;;;;;;;;;; i=3 to n
	a=factor(i)
	msg=strcat(i,"=",a)
	FileWrite(h,msg)
next
bbbb=GetTickCount()
cccc=(bbbb-aaaa)/1000
msg=strcat("Time in seconds = ",cccc)
FileWrite(h,msg)
FileClose(h)
Message("ALL","Done")


Article ID:   W15576
File Created: 2003:05:13:11:29:06
Last Updated: 2003:05:13:11:29:06