comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Recursive Function Program in IDL
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Recursive Function Program in IDL [message #46701] Wed, 14 December 2005 09:50
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
R.G. Stockwell writes:

> An old program in my hackware style of programming.
> Honest, I program much better nowadays!!
>
> This finds the prime factors of a number (i.e. for 9, it returns 3, 3).
> Very useful for making sure you pass a factorable length time series to
> an fft routine ( if time is important to you).

Gosh, maybe I'll make a collection of these and post them.
It will probably keep other people from crossing their eyes
and sticking their tongues out the corner of their mouths when
they try to write a recursive function. Thanks!

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Recursive Function Program in IDL [message #46702 is a reply to message #46701] Wed, 14 December 2005 09:46 Go to previous message
news.qwest.net is currently offline  news.qwest.net
Messages: 137
Registered: September 2005
Senior Member
"David Fanning" <davidf@dfanning.com> wrote in message
news:MPG.1e09eff8986d3ab0989ae7@news.frii.com...
> Folks,
>
> Does anyone have a handy recursive function that does something neat?
> Someone is asking, and I don't have time to work on this. He
> (apparently) can't get to the newsgroup.
>
> Thanks,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming: http://www.dfanning.com/


An old program in my hackware style of programming.
Honest, I program much better nowadays!!

This finds the prime factors of a number (i.e. for 9, it returns 3, 3).
Very useful for making sure you pass a factorable length time series to
an fft routine ( if time is important to you).

Cheers,
bob




; find the factors of a number

function factors, n,prevfactors=prevfactors


maxfactor = fix(sqrt(n))
if maxfactor le 1 then begin
if keyword_set(prevfactors) then begin
prevfactors = [prevfactors,n]
return,n
endif else begin
return,n
endelse
endif

fac = findgen(maxfactor-1)+2 ; 2 -- sqrt(n)

doloop = 1
factorflag = 0
counter = 0


while doloop do begin
if n mod fac(counter) eq 0 then begin
factorflag = 1
newfactor = fac(counter)
if keyword_set(prevfactors) then prevfactors = [prevfactors,newfactor] $
else prevfactors = newfactor
newnumber = n/newfactor
; to iterate is human, to recurse is divine
r = factors(newnumber,prevfactors=prevfactors)
doloop = 0
endif
counter = counter+1
if counter ge maxfactor-1 then doloop = 0
endwhile


if n_elements(prevfactors) eq 0 then prevfactors = n else begin
; only if n is prime do we add it here
if not(factorflag) then prevfactors = [prevfactors,n]
endelse




return,prevfactors


end


;;;;___________ test code here ___________________

n = 5001

r = factors(n)


print
print
print,'Finished calculating factors_______'
print,'Number: ',n
print,'Factors:'
print,r



end
Re: Recursive Function Program in IDL [message #46704 is a reply to message #46702] Wed, 14 December 2005 09:16 Go to previous message
Antonio Santiago is currently offline  Antonio Santiago
Messages: 201
Registered: February 2004
Senior Member
David Fanning wrote:
> Folks,
>
> Does anyone have a handy recursive function that does something neat?
> Someone is asking, and I don't have time to work on this. He
> (apparently) can't get to the newsgroup.
>
> Thanks,
>
> David
>

Working with trees are almos always a good place to work with recursion:

http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search


--
-----------------------------------------------------
Antonio Santiago P�rez
( email: santiago<<at>>grahi.upc.edu )
( www: http://www.grahi.upc.edu/santiago )
( www: http://asantiago.blogsite.org )
-----------------------------------------------------
GRAHI - Grup de Recerca Aplicada en Hidrometeorologia
Universitat Polit�cnica de Catalunya
-----------------------------------------------------
Re: Recursive Function Program in IDL [message #46708 is a reply to message #46704] Wed, 14 December 2005 08:51 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Paolo Grigis writes:

> this computes the binomial coefficients recursively
> (the numbers showing up in Pascal's triangle)

Thanks, Paolo.

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Recursive Function Program in IDL [message #46709 is a reply to message #46708] Wed, 14 December 2005 08:36 Go to previous message
Paolo Grigis is currently offline  Paolo Grigis
Messages: 171
Registered: December 2003
Senior Member
this computes the binomial coefficients recursively
(the numbers showing up in Pascal's triangle)

FUNCTION binomial,n,j

IF n LT j THEN BEGIN
print,'INVALID INPUT IN BINOMIAL'
RETURN,-1
ENDIF

IF j LE 0 THEN RETURN,1

IF j EQ n THEN $
RETURN,1 ELSE $
RETURN,binomial(n-1,j)+binomial(n-1,j-1)

END

Ciao,
Paolo

David Fanning wrote:
> Folks,
>
> Does anyone have a handy recursive function that does something neat?
> Someone is asking, and I don't have time to work on this. He
> (apparently) can't get to the newsgroup.
>
> Thanks,
>
> David
>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Fun Writing Books
Next Topic: Re: Polygon Clipping Algo in IDL

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 07:36:07 PDT 2025

Total time taken to generate the page: 1.19962 seconds