I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}
Related
<?php
if (isset($_POST['submit'])) {
$A = $_POST['num1'];
$a = $_POST['num1'];
if (isset($_POST['num2'], $_POST['num3'])) {
$kaha_se = $_POST['num2'];
$kaha_tak = $_POST['num3'];
$limit = $kaha_tak * 10;
while ($A <= $limit && $kaha_se <= $kaha_tak) {
$A = $a * $kaha_se;
echo "$a X $kaha_se = $A <br>";
$A += $a;
$kaha_se++;
}
} else {
$J = 1;
$S = $a * 10;
while ($A <= $S && $J <= 10) {
echo "$a X $J = $A <br>";
$A += $a;
$J++;
}
}
}
?>
The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.
I'm currently trying out PHP and I'm doing some exercises a friend told me but I can't seem to make the following:
98765
8765
765
65
5
I already made the following code:
<?php
for ($a = 9; $a >= 5; $a–-) {
for ($b = 1; $b <= $a; $b++) {
echo $a;
}
echo "<br>";
}
?>
Any tips / fixes?
Your code should be:
<?php
for($a=9; $a >= 5; $a--) {
for ($b=$a; $b >= 5; $b--) {
echo $b;
}
echo "\n";
}
<?php
function print_triangle($start, $count) {
for ($a = 0; $a < $count; $a++) {
for ($b = $start - $a; $b > $start - $count; $b--) {
echo $b;
}
echo "<br>";
}
}
print_triangle(9, 5);
?>
Hope it helps!
Well, I fixed it by creating the following code:
for ($a = 0; $a <= 9; $a++) {
for ($b = 9 - $a; $b >= 5; $b--) {
echo $b;
}
echo "<br>";
}
?>
Thanks for at least answering guys!
i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.
what i did:
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
}
When i run this code i will not get anything.
What is the reason for this?
Thank you!
Here is code-
<?php
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
$i++;
}
You need to add the index of the array you are adding to or you are just writing over it.
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[$counter] = array($a,$b);
$counter++;
}
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
if ($a != $b)
echo $a.' is not '.$b;
else
echo $a.'='.$b;
$i++;
}
The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>
The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.
The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing