如何找到最高的5个数字序列[关闭]

I have this large string of digits:

$number = "8457714236241996394662789446446900947844174208436229465708413283480252308452309445136821833388596362971481609898423885940334748909310590559808872696932821556902064811261967616862184374009875724727718022667764327386516693129548512719134416243929994231994949382960356875888556457289812078753217535142126801211014214834504421933614164095445014707761460891338932623869783278796726454120129597585667668220935747858847437582800078002228689051590879937597834754848624741161377581359919801031273163364064818325258392890356143251007563549777054047013154878246432673611603091925150515368164497803349723366818641042829792279918398010084882019400234971502609827514132560077285356201267749465601230620222274625799272352699855298156629748948075668465722353974463067725471326703029315330935768523979276352367926340029415999734624584287325909545543547035525815285397244049320738084432091566581396643275969932094091735878708555576059445021204851541525428226299437613379041397828790357002353728103827178553574459018352";

I want to find a sequence of 5 numbers which creates the highest number. I supposed I have to iterate at all numbers then if the sum of numbers is bigger than previous one I have to save it and check it again, but with minus 1 previous and + 1 next. I just have no idea how to put this into script. How can I do it?

First, you need to make a loop. The loop needs to execute a number of times equal to the length of the input string minus the length of the substring you want to evaluate. Calculate that length before constructing the loop.

$digits = 5;
$count = strlen($number) - $digits;

Then initialize a maximum value. Loop from zero to the count you calculated, and take substrings starting from the position indicated by your loop increment variable. Compare them to your previous maximum value and overwrite that value with the current substring if it is greater.

$max = 0;

for ($i = 0; $i <= $count; $i++) {
    $substr = substr($number, $i, $digits);
    $max = max($max, $substr);
}