Re: Learning recursion with hailstone seqence
- From: "Tom de Neef" <tdeneef@xxxxxxxx>
- Date: Fri, 17 Nov 2006 19:05:40 +0100
"DAVID B MORGAN" <lepton2@xxxxxxxxxxx> schreef in bericht
news:J%47h.3638$v93.2468@xxxxxxxxxxx
I'm playing with the hailstone sequence. My function will
need to iterate millions of times to process the data. I have two
questions:
1. Can this function be optimised ? (approx 2^67 iterations are neccessary
on current dataset)
2. How can I test huge numbers (like 2^1000) --- Int64 can't handle it
function hailstone(x:int64):int64;
begin
if odd(x) then result:=x*3+1
else result:=x shr 1;
if result<>1 then hailstone(result) ;
end;
If the purpose is to exercise with recursion, then consider this function
instead:
function hailstone(x:int64):int64;
// will return iteration length
begin
if odd(x) then x:=x*3+1 else x:=x shr 1;
if x>1 then result:=1+hailstone(x) else result:=0;
end;
Count on a recursion depth of 50 times the bit-length of x.
Tom
.
- References:
- Learning recursion with hailstone seqence
- From: DAVID B MORGAN
- Learning recursion with hailstone seqence
- Prev by Date: Re: Out of system resources
- Next by Date: Re: Out of system resources
- Previous by thread: Re: Learning recursion with hailstone seqence
- Next by thread: Re: Learning recursion with hailstone seqence
- Index(es):
Relevant Pages
|