Page 1 of 1

Recursive help

Posted: Sun May 16, 2010 1:10 pm
by cocenpt2008
i have been trying to figure this out for a day now.

i used to have a great example of this but i cannot find it anywhere. i have tried to recreate this but for some reason i cannot.

i need a recursive number generator. i cannot have it random.

I need to be able to set the amount of numbers 2-20 and i want to be able to set the range of the numbers 1-115

this is really stupid i know but i cannot figure this out for the life of me in a nice clean few lines of code like i used to have.

thanks

Re: Recursive help

Posted: Sun May 16, 2010 1:21 pm
by requinix
So if it's not random, how does it choose which numbers to pick?

Re: Recursive help

Posted: Sun May 16, 2010 6:30 pm
by cocenpt2008
well i want it to go in a recursive order

for example i would like the output to be something like this

1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 3
1 1 1 1 1 1 1 1 4
1 1 1 1 1 1 1 2 1
1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 1 2 3
1 1 1 1 1 1 1 2 4

basically i would like it to work something like that so the code recursively changed all the numbers so that every possible number combination is generated

9 total numbers for this example.
each number can be a range from 1-115 but for this example i used 1-4

Re: Recursive help

Posted: Sun May 16, 2010 7:06 pm
by AbraCadaver
And the practical application for this function would be what, homework? :wink:

Re: Recursive help

Posted: Sun May 16, 2010 7:21 pm
by cocenpt2008
lol no i am 28 school is a long way back in my book of life which is a very short and non interesting read.

and if i told you the reason i want this is that would spoil the project but i will tell you what it is not for it is not for homework or password cracking or anything like that. i am actually using this for statistics research. and to fully to this i need to make every possible number chain. and then run it through a statistics algo of my own creation.

Re: Recursive help

Posted: Sun May 16, 2010 9:19 pm
by Eran
If you are familiar with recursive formulas from basic algebra, it shouldn't be difficult. This is more a math problem than a programming problem.

http://mathworld.wolfram.com/RecursiveSequence.html

Re: Recursive help

Posted: Sun May 16, 2010 9:29 pm
by requinix
You do know you'll run out of memory very quickly, right? At four bytes per number, 9 numbers from 1-9 is almost 13GB, and that doesn't count the memory PHP needs to remember all 387 million arrays...

20 numbers from 1-115 is about 7.01 * 10^34 gigabytes in 1.64 * 10^41 arrays. They don't even have prefixes for numbers that large...

Re: Recursive help

Posted: Sun May 16, 2010 11:12 pm
by cocenpt2008
i was not going to store everything for the entire scope of all iterations.

if i simply output all possibilities and run each one as its created through my statistical formula save the results if they are better then the last and move on.

so really only a little bit of memory is allocated as long as all results are processed and cleared before the next one is created

Re: Recursive help

Posted: Mon May 17, 2010 1:52 am
by requinix
Ah, just outputting. Good.

You only need one function, but it has to know how many numbers to print, the range for those numbers, and the sequence of numbers up to that point.

Looping from the bottom to the top of the range, it adds the number onto a copy of that old sequence and (a) if it's the last in the sequence (no more to print after this) it prints the whole sequence and stops, otherwise (b) it calls itself recursively but with one less number to generate and the modified sequence copy instead of the original.

Re: Recursive help

Posted: Mon May 17, 2010 2:01 am
by cocenpt2008
i kinda understand what you mean and i came up with this but its all wrong and i know it,

Code: Select all

<?php



donumbers("",5);

function donumbers($base,$level){

echo $base;

for($y=0;$y<$level;$y++){
$alpha[$y] = $y;
}
if ($level>0){


for ($x=0;$x<6;$x++){
print_r($alpha);
echo "<br>";

donumbers($alpha[$x],$level - 1);

}

}

}



?>
the output is all wrong i am missing something here and i know my logic is not correct.

thanks for any help

Re: Recursive help

Posted: Mon May 17, 2010 2:21 am
by requinix
A little nudge in the right direction?

Code: Select all

function donumbers(howmany, low, high, sofar = array())
	for i between low and high
		sofar_copy = sofar
		sofar_copy[] = i

		if howmany = 1 then // last one
			print sofar_copy
		else
			donumbers(howmany - 1, low, high, sofar_copy)
		end if
	end for
end function

Re: Recursive help

Posted: Mon May 17, 2010 6:03 am
by cocenpt2008
Thank you so much for your help. you have got me a great deal further then i was before but after putting the code you created i came up with some problems i might have screwed up somewhere along the way.

here is what i got

Code: Select all

donumbers(3,1,3,$sofar);
function donumbers($howmany, $low, $high, $sofar){
$n=0;
for($i=0;$i>=$low,$i<=$high;$i++){
$sofar_copy = $sofar;
$sofar_copy[$n] = $i;
$n++;
if ($howmany == 0){
$count = count($sofar_copy);
if($count == 3){
print_r($sofar_copy);
echo "<br>";
}// end if count statment
}else{
donumbers($howmany - 1, $low, $high , $sofar_copy);
}// end if howmany == 0 including else statment

}// end for statment
}// end function
here is some output from the script and as you can see its not hitting every possible number combination as you can see it fails right from the start and never generates the sequence

1 1 1
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [2] => 2 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [1] => 1 [3] => 3 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [1] => 1 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [2] => 2 [3] => 3 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [0] => 0 [3] => 3 [1] => 1 )
Array ( [0] => 0 [3] => 3 [2] => 2 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [0] => 0 [2] => 2 )
Array ( [1] => 1 [0] => 0 [3] => 3 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [2] => 2 [0] => 0 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [2] => 2 [3] => 3 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [1] => 1 [3] => 3 [0] => 0 )
Array ( [1] => 1 [3] => 3 [2] => 2 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [0] => 0 [1] => 1 )
Array ( [2] => 2 [0] => 0 [3] => 3 )
Array ( [2] => 2 [1] => 1 [0] => 0 )
Array ( [2] => 2 [1] => 1 [3] => 3 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [2] => 2 [3] => 3 [0] => 0 )
Array ( [2] => 2 [3] => 3 [1] => 1 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [1] => 1 )
Array ( [3] => 3 [2] => 2 [1] => 1 )
Array ( [3] => 3 [2] => 2 [1] => 1 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [1] => 1 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [1] => 1 )
Array ( [3] => 3 [0] => 0 [1] => 1 )
Array ( [3] => 3 [0] => 0 [2] => 2 )
Array ( [3] => 3 [1] => 1 [0] => 0 )
Array ( [3] => 3 [1] => 1 [2] => 2 )
Array ( [3] => 3 [2] => 2 [0] => 0 )
Array ( [3] => 3 [2] => 2 [1] => 1 )