Creating 1000 digit within 10 loops only - php
Is it possible to create 1000 numbers from 0000 to 9999 using only 10 loops?
for ($i=0; $i < 10; $i++)
{
# code...
$numberList .= "";
}
echo $numberList;
result: 0000, 0001, 0002, ... 9999
UPDATES
I need to minimize the code because I have a not so good server.
I am working on huge list of numbers.
Example: 1000-1999, 2000-2999, ... 9000-9999
and also xxx-1000 to xxx-19999, xxx-2000 to xxx-2999, ... xxx-9000 to xxx-9999
Is it helpful to do it like this?
$numberList = array();
for($i = 0; i < 10; i++){
for($j = 0; j < 10; j++){
for($k = 0; k < 10; k++){
for($l = 0; l < 10; l++){
$numberList[] = i . j . k . l;
}
}
}
}
In this way you will have all 4 digits even the most significant zeros like '0001'.
Related
Print triangular number patterns with loops and modulus calculation
I need to print a number pattern like this: 1 12 123 1234 2 23 234 2341 3 34 341 3412 4 41 412 4123 My code: for($i=1; $i<=4; ++$i) { for($j=1; $j<=$i; ++$j) { echo $j; } echo ("<br/>"); } for($i=2; $i<=4; ++$i) { for($j=2; $j<=$i; ++$j) { echo $j; } echo ("<br/>"); } I don't know how to recycle to the first number after the max number is reached. Since my max number is 4, 1 should used instead of 5 (and 2 for 6 and 3 for 7).
You loop from 1 to 4, and subtract 4 if the value is bigger than 4. This is for 2, 23, 234, 2341: for ($i = 1; $i <= 4; $i++) { for ($j = 1; $j <= $i; $j++) { $value = $j + 1; // or +2, or +3 echo $value > 4 ? $value - 4 : $value; } echo "\n"; } And this would generate all output within one big loop: $max = 4; for ($start = 0; $start < $max; $start++) { for ($i = 1; $i <= $max; $i++) { for ($j = 1; $j <= $i; $j++) { $value = $j + $start; echo $value > $max ? $value - $max : $value; } echo "\n"; } }
Whenever you need circular array access, it is a good idea to implement a modulus calculation. To set this up, use a range between 0 and 3 instead of using 1 and 4. A modulus calculation can return a 0 result, so the formula must be prepared to handle this value. To adjust the value, just add 1 after the modulus calculation to generate numbers between 1 and 4. Accumulate strings of numbers as desired and reset the string upon each start of the outer loop. Below proves that you do not need three nested loops for this task. Code: (Demo) for ($i = 0; $i < 4; ++$i) { for ($s = '', $j = 0; $j < 4; ++$j) { $s .= ($i + $j) % 4 + 1; echo $s . "\n"; } } Output: 1 12 123 1234 2 23 234 2341 3 34 341 3412 4 41 412 4123
PHP. Assigning values to an array within a loop
OK, simple question. New to PHP. How do I accomplish something similar to the following in PHP? This is no specific language intentionally. i = 0; j = 0; k = 50; While i <= 5 While j <= 10 myarray(i,j) = k i = i + 1 j = j + 1 k = k + 2 next next I have found much info regarding arrays and loops in PHP and none seem to accomplish this simple task. Please don't be snarky... just give me a hand here.
You start each inner while j<=10. Note that you didn't set $j to 0. So correct the code is follows: $i = 0; $k = 50; While( $i <= 5 ) { $j = 0; While( $j <= 10) { $myarray[$i][$j] = $k; $j++; $k = $k + 2; } $i++; }
Your pseudo code leaves some ambiguities, but maybe you mean to do the following: $k=50; for ($i=0; $i<=5; $i++) for ($j=0; $j<=10; $j++, $k+=2) $myarray[$i][$j]=$k; This is a nested for-loop. The actual assignment to $myarray will happen 6*11=66 times in total and $k will have values from 50 to 115. In PHP arrays don't need to be declared. You can simply assign a value to an arbitrary index of a given variable. Here is a little demo.
Find the highest product in 4 directions in a matrix
I got this challenge to find the highest product of 4 consecutive numbers on a 20x20 matrix of integers. The numbers are read line by line from a file separated by a space. The products can be in horizontal, vertical and diagonal in both directions My "solution" gives the wrong answer. EDIT: I've updated the code to work without file input and added sample data; also fixed one of my mistakes that were pointed out in the comments $data = [ [89,32,92,64,81,2,20,33,44,1,70,75,39,62,76,35,16,77,22,27], [53,11,6,95,41,51,31,59,8,23,19,13,61,91,48,69,84,52,66,24], [93,72,85,97,21,79,56,5,45,3,65,30,83,87,43,7,34,0,4,14], [29,17,49,9,82,90,55,67,15,63,54,94,12,28,96,37,58,98,86,78], [74,40,50,60,26,99,80,18,10,46,36,68,25,57,47,71,42,73,88,38], [50,22,6,26,18,53,52,5,46,2,89,77,83,48,4,58,45,28,84,81], [49,82,31,14,69,17,91,54,34,40,0,33,30,95,60,44,29,24,85,16], [27,11,76,39,15,86,92,74,99,59,94,12,55,57,38,96,47,32,78,75], [51,20,87,42,62,41,7,35,23,21,71,25,67,97,80,90,88,64,13,70], [19,9,56,43,68,93,65,98,36,3,61,63,10,72,8,73,1,66,79,37], [22,58,52,12,3,41,28,72,42,74,76,64,59,35,85,78,14,27,53,88], [46,80,5,96,7,68,61,69,67,34,36,40,82,26,75,50,29,91,10,2], [30,39,19,48,33,93,1,45,66,98,0,23,62,25,51,71,56,77,24,21], [79,87,94,60,8,32,13,65,4,92,73,9,31,37,17,84,15,90,86,20], [95,6,81,70,47,16,44,83,49,43,55,54,18,63,38,11,97,89,99,57], [95,78,64,58,7,17,53,28,74,86,6,12,54,85,21,94,16,69,25,68], [13,20,41,97,1,2,80,30,0,84,67,45,93,96,82,92,62,33,18,44], [60,77,31,70,76,36,59,38,15,3,91,46,65,73,49,11,8,35,5,52], [61,66,79,40,26,72,89,71,75,99,22,9,43,32,14,81,98,88,87,83], [10,4,23,19,56,57,51,47,50,27,90,63,42,29,24,55,48,37,39,34] ]; $matrix = []; //maximums in possible directions $maxes = [0, 0, 0, 0]; //while ($line = trim(fgets(STDIN))) { while ($line = current($data)) { //the horizontal maxes can be calculated while loading //$array = explode(" ", $line); $array = $line; $hMax = array_product(array_slice($array, 0, 4)); for ($i = 1; $i < (count($array)-4); $i++) { $max = array_product(array_slice($array, $i, 4)); if($max > $hMax) { $hMax = $max; } } if ( $hMax > $maxes[0] ) { $maxes[0] = $hMax; } $matrix[] = $array; next($data); } // the last 3 rows can be skipped for($i = 0; $i < (count($matrix)-4); $i++) { for ($j = 0; $j < (count($matrix[$i])-1); $j++) { $vMax = 1; // vertical $dlMax = 1; // diagonal left $drMax = 1; // diagonal rigth for ($k = 0; $k < 5; $k++) { $vMax *= $matrix[$i + $k][$j]; if ( $j < (count($matrix[$i]) - 4) ) { $drMax *= $matrix[$i + $k][$j + $k]; } if ( $j > 3 ) { $dlMax *= $matrix[$i + $k][$j - $k]; } } if ( $maxes[1] < $vMax ) $maxes[1] = $vMax; // the index used to be 1 - my first mistake if ( $maxes[2] < $dlMax ) $maxes[2] = $dlMax; // the index used to be 1 - my first mistake if ( $maxes[3] < $drMax ) $maxes[3] = $drMax; // the index used to be 1 - my first mistake } } sort($maxes); echo end($maxes).PHP_EOL; Where did my approach go wrong, and how can it be sped up? Are there any math tricks that can be applied here (besides checking for zeros)? EDIT: the solution that the code gives for the current data is 4912231320 is it correct?
I've found 2 major errors, and now the result is a plausible 67352832 I'm considering it solved for that reason, but if anyone comes up with some math trick that simplifies or makes it faster I'll give up the accepted answer. The first mistake was for ($k = 0; $k < 5; $k++) { It should've been for ($k = 0; $k < 4; $k++) { since we are only counting 4 numbers at once, thats why the result was so large compared to 10^8 The second was if ( $j > 3 ) { which should've been if ( $j > 2 ) { which will now include one more diagonal possibility
We can consider the four directions a bottom- or right-most cell can be the last of in a sequence. If m[i][j][k][d] is the highest total for a sequence of length k coming from direction d, then: m[i][j][1][d] = data[i][j] for all d m[i][j][k]['E'] = data[i][j] * m[i][j - 1][k - 1]['E'] m[i][j][k]['NE'] = data[i][j] * m[i - 1][j - 1][k - 1]['NE'] m[i][j][k]['N'] = data[i][j] * m[i - 1][j][k - 1]['N'] m[i][j][k]['NW'] = data[i][j] * m[i - 1][j + 1][k - 1]['NW'] If we traverse north to south, east to west, the needed cells should have already been calculated, and, clearly, we're looking for max(m[i][j][4][d]) for all i, j, d
PHP Dubble For Loop
I need to make a loop in php that says 1*9 = 9 2*9 = 18 and 1*8 = 8 and 1*7 = 7 etc in loop so the result will be this: 1*7 = 7 2*7 = 14 3*7 = 21 1*8 = 8 2*8 = 16 3*8 = 24 1*9 = 9 2*9 = 18 3*9 = 27 And I need to use a nested loop for that i tried some things out but i cant get it to work here is what is did. But for the most part i don't really understand what nested loops do and what there used for. hope you can help me thanks! for ($tafel7 = 0; $tafel7 <= 9; $tafel7++) { for($tafel7 = 0; $tafel7 <= 9; $tafel7++){ for($tafel7 = 0; $tafel7 <= 9; $tafel7++){ $antwoord9 = $tafel7 * 9; echo "$tafel7 x 8 = $antwoord9 <br>"; $antwoord8 = $tafel7 * 8; echo "$tafel7 x 8 = $antwoord8 <br>"; $antwoord7 = $tafel7 * 7; echo "$tafel7 x 7 = $antwoord7 <br>"; }; }; };
Your question is not as clear but if you just want the exact result, here's the code: for($i = 7; $i <= 9; $i++){ for($j = 1; $j <= 3; $j++){ $result = $i * $j; echo $j." * ".$i." = ".$result."<br>"; } } If you need to print complete tables, then try this: for($i = 1; $i <= 9; $i++){ for($j = 1; $j <= 10; $j++){ $result = $i * $j; echo $i." * ".$j." = ".$result."<br>"; } } As far as the explanation is concerned, then listen: A normal loop works by executing a block of statement(s) again and again until a condition is met. So, nested loops are basically loops in another loop. This means that the block of code will be executed 'n' number of times by the inner loop, and then the inner loop will again be executed by the outer loop, again 'n' number of times. 'n' means whatever the number of times the conditions are met for the respective for loops. Hope you got my explanation!
You need to loop over your two variables, (1) the left operand and (2) the right operand for ($right = 7; $right <= 9; $right++) { for ($left = 1; $left <= 3; $left++) { $product = $left * $right; echo "{$left} x {$right} = {$product} <br>"; } }
In this example, I have to loops one with the times table you want eg 7, 8 and 9 "tafel" and the other one with how many times you want it to run for each times table "$hoeveel". This can easily be altered to more times tables or how many times you want to run, you also only need to export the values once as it cycles through the loops anyways. Hope it helps. for ($tafel = 7; $tafel <= 9; $tafel++) { for ($hoeveel = 1; $hoeveel <= 3; $hoeveel++) { $antwoord = $tafel * $hoeveel; echo "$tafel x $hoeveel = $antwoord <br>"; } }
Simple PHP program requires less time to execute
i had applied for a job recently and the requirement was to complete a test and then interview 2 questions were given for test which was very simple and i did it successfully but still i was told that i have failed the test because the script took more than 18 seconds to complete execution. here is the program i dont understand what else i could do to make it fast. although i have failed the test but still wants to know else i could do? Program language is PHP and i had to do it using command line input here is the question: K Difference Given N numbers , [N<=10^5] we need to count the total pairs of numbers that have a difference of K. [K>0 and K<1e9] Input Format: 1st line contains N & K (integers). 2nd line contains N numbers of the set. All the N numbers are assured to be distinct. Output Format: One integer saying the no of pairs of numbers that have a diff K. Sample Input #00: 5 2 1 5 3 4 2 Sample Output #00:3 Sample Input #01: 10 1 363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 Sample Output #01: 0 Note: Java/C# code should be in a class named "Solution" Read input from STDIN and write output to STDOUT. and this is the solution $fr = fopen("php://stdin", "r"); $fw = fopen("php://stdout", "w"); fscanf($fr, "%d", $total_nums); fscanf($fr, "%d", $diff); $ary_nums = array(); for ($i = 0; $i < $total_nums; $i++) { fscanf($fr, "%d", $ary_nums[$i]); } $count = 0; sort($ary_nums); for ($i = $total_nums - 1; $i > 0; $i--) { for ($j = $i - 1; $j >= 0; $j--) { if ($ary_nums[$i] - $ary_nums[$j] == $diff) { $count++; $j = 0; } } } fprintf($fw, "%d", $count);
Your algorithm's runtime is O(N^2) that is approximately 10^5 * 10^5 = 10^10. With some basic observation it can be reduced to O(NlgN) which is approximately 10^5*16 = 1.6*10^6 only. Algorithm: Sort the array ary_nums. for every i'th integer of the array, make a binary search to find if ary_nums[i]-K, is present in the array or not. If present increase result, skip i'th integer otherwise. sort($ary_nums); for ($i = $total_nums - 1; $i > 0; $i--) { $hi = $i-1; $low = 0; while($hi>=$low){ $mid = ($hi+$low)/2; if($ary_nums[$mid]==$ary_nums[$i]-$diff){ $count++; break; } if($ary_nums[$mid]<$ary_nums[$i]-$diff){ $low = $mid+1; } else{ $hi = $mid-1; } } } }
I got the same question for my technical interview. I wonder if we are interviewing for the same company. :) Anyway, here is my answer I came up with (after the interview): // Insert code to get the input here $count = 0; sort ($arr); for ($i = 0, $max = $N - 1; $i < $max; $i++) { $lower_limit = $i + 1; $upper_limit = $max; while ($lower_limit <= $upper_limit) { $midpoint = ceil (($lower_limit + $upper_limit) / 2); $diff = $arr[$midpoint] - $arr[$i]; if ($diff == $K) { // Found it. Increment the count and break the inner loop. $count++; $lower_limit = $upper_limit + 1; } elseif ($diff < $K) { // Search to the right of midpoint $lower_limit = $midpoint + 1; } else { // Search to the left of midpoint $upper_limit = $midpoint - 1; } } } #Fallen: Your code failed for the following inputs: Enter the numbers N and K: 10 3 Enter numbers for the set: 1 2 3 4 5 6 7 8 9 10 Result: 6 I think it has to do with your calculation of $mid (not accounting for odd number)