How can i generate a number pattern like this using php?
a. Start = 1, End = 3 123 231 312 b. Start = 2 , End = 7 234567 345672 456723 567234 672345 723456
UPDATE: I tried this code:
function generate (int $start, int $end) { $arr = []; for($start; $start <= $end; $start ++) { $arr[] = $start; } for($i = $arr[0]; $i <= count($arr); $i++) { for($l = $i - 1; $l < $end; $l ++) { echo $arr[$l]; } echo " -> $i<br/>"; } }
and get this output:
12345 2345 345 45 5
But how to show the rest numbers?
Advertisement
Answer
you can try this algorithm:
const generate = (start, end) => { const length = end-start+1 let array = Array.from({length}, () => Array.from({length}, (x,i)=>i+start)) // creating 2D array and filling it with a loop from start value to end value for (let i = 0; i < array.length; i++) { poped = array[i].splice(i); // slice and put the element from index i to the last index array[i].unshift(...poped) // adding poped value to the begining of the array } return array } console.log(generate(1,3)) console.log(generate(2,7))