How to test if $value is $num +-3 [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php if integer between a range?
Let's say $num = 5; How do I test if $value is anything in between +-3 of $num. In other words, how can I test if $value is equal to any of these values 2,3,4 5 6,7,8

Two possible ways to do it:
$num - 3 <= $value && value <= $num + 3
abs($num - $value) <= 3

$mid = 5;
$range = 3;
$inRange = ($myval>=$mid-$range && $myval<=$mid+$range) ? TRUE : FALSE;
UPDATE
I started throwin' out bass, she started throwin' back mid-range.

if ($num - 3 <= $value && $value <= $num + 3)

if ($value<=$num+3 && $value>=$num-3)
echo "$value is between +-3 of $num";
else
echo "$value is outside +-3 of $num";

try this:
$dif1 = $num - 3;
$dif2 = $num + 3;
if($dif1 <= $value){
if($dif2 <= $value){
echo "Your number in between +-3";
}
}

There is nothing big logic in this if you know the $num value take two variable $min and $max
set $min = $num - 3
set $max = $num + 3
and then with condition check your value..
$value > $min && $value < $max

Related

Print 2 items of array per line and separate by comma [duplicate]

This question already has answers here:
Modulus in a PHP loop
(7 answers)
Closed 8 months ago.
can u please help me on this. I have an array with 6 items, I want to print 2 items per line separated by commas, I have this code:
$rates[] = array("2020-01-01"=>3000);
$rates[] = array("2020-01-02"=>3010);
$rates[] = array("2020-01-03"=>3020);
$rates[] = array("2020-01-04"=>3021);
$rates[] = array("2020-01-05"=>3030);
$rates[] = array("2020-01-06"=>3035);
$rates_count = count($rates);
$total = $rates_count;
$col = 2;
for ($i = 0; $i < $total; $i++) {
$date = key($rates[$i]);
$value = end($rates[$i]);
if ($i % $col != 0) {
echo "{$date},";
echo "{$value},";
} else {
echo "{$date},";
echo "{$value}";
echo "</br>";
}
}
This code print this:
2020-01-01,3000
2020-01-02,3010,2020-01-03,3020
2020-01-04,3021,2020-01-05,3030
2020-01-06,3035,
I want this:
2020-01-01,3000,2020-01-02,3010
2020-01-03,3020,2020-01-04,3021
2020-01-05,3030,2020-01-06,3035
Where is the error? Thanks for any help.
I tested this and your output should be correct now
if ($i % $col == 0) {
echo "{$date},";
echo "{$value},";
} else {
echo "{$date},";
echo "{$value}";
echo "</br>";
}
Output:
2020-01-01,3000,2020-01-02,3010
2020-01-03,3020,2020-01-04,3021
2020-01-05,3030,2020-01-06,3035
I changed the comparison to be " == 0"
The first value in the iteration will be cero because 0 % 2 = 0
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
... and so on
The condition was returing false in the first iteration, it was fixed by changing the comparison of the remainder to be equal to cero.

php - rand() between the same values

I havn't found a solution for this.
I have the numbers 3 and 5.
How do I randomly choose one of those two numbers.
In PHP.
rand() or mt_rand() jsut has min and max parameters.
thank you for your help!
if(mt_rand(0,1)) {
echo 3;
} else {
echo 5;
}
Or reverse it, your choice.
Get a random number with rand() and set $value to it's new value depending on the random number.
$value = rand(0,1) == 0 ? 3 : 5;
Just do a rand between only 2 values then use an if. So:
$randval = rand(0,1);
if($randval == 0)
$value = 3;
else
$value = 5;
You know that you have 2 values so you can do something like this:
$rand = rand(1,2);
if ($rand == 1)
$val = 3;
else
$val = 5;

php while $count, want to add all the result

Im facing some issue on counting total number of output.
<?php
$count = 1;
while ($count <= 10)
{
echo "$count ";
++$count;
}
?>
Result output
1 2 3 4 5 6 7 8 9 10
so what i want is to add all the result that is
1+2+3+4+5+6+7+8+9+10 = ? in my same code?
Try
$count = 1;
$add=0;
while ($count <= 10)
{
$add=$add+$count;
echo "$count ";
++$count;
}
$count = 1;
$countall = 0;
while ($count <= 10)
{
echo "$count ";
$countall=$countall+$count;
$count++;
}
echo "$countall";
try this
Simply use the range function and array_sum to get the result
array_sum(range(1,10))
Evidently not what you are looking for, but if what you need is to calculate a summation, you can use this formula:
Using it you can calculate the result of adding all the values of $count in this code:
<?php
$count = 1;
while ($count <= $n)
{
echo $count.' ';
++$count;
}
?>
That will be:
<?php
$result = $n * ($n + 1) / 2;
?>
Which for $n = 10 is 55.

how to find the sum of all the multiples of 3 or 5 below 1000 in php, issue?

i have an small issue with the way this problem is resolved.
some would say: println((0 /: ((0 until 1000).filter(x => x % 3 == 0 || x % 5 == 0))) (_+_)) is the solution witch adds to 233168
my way was to do:
$maxnumber = 1000;
for ($i = 3; $i < $maxnumber; $i += 3)
{
$t += $i;
echo $i.',';
}
echo '<br>';
for ($j = 5; $j < $maxnumber; $j += 5)
{
$d += $j;
echo $j.',';
}
echo '<br>';
echo $t;
echo '<br>';
echo $d;
echo '<br>';
echo $t+$d;
this will give me :
3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312,315,318,321,324,327,330,333,336,339,342,345,348,351,354,357,360,363,366,369,372,375,378,381,384,387,390,393,396,399,402,405,408,411,414,417,420,423,426,429,432,435,438,441,444,447,450,453,456,459,462,465,468,471,474,477,480,483,486,489,492,495,498,501,504,507,510,513,516,519,522,525,528,531,534,537,540,543,546,549,552,555,558,561,564,567,570,573,576,579,582,585,588,591,594,597,600,603,606,609,612,615,618,621,624,627,630,633,636,639,642,645,648,651,654,657,660,663,666,669,672,675,678,681,684,687,690,693,696,699,702,705,708,711,714,717,720,723,726,729,732,735,738,741,744,747,750,753,756,759,762,765,768,771,774,777,780,783,786,789,792,795,798,801,804,807,810,813,816,819,822,825,828,831,834,837,840,843,846,849,852,855,858,861,864,867,870,873,876,879,882,885,888,891,894,897,900,903,906,909,912,915,918,921,924,927,930,933,936,939,942,945,948,951,954,957,960,963,966,969,972,975,978,981,984,987,990,993,996,999
5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995
$t - 166833
$d - 99500
and total:
266333
why am i wrong?
Some numbers are multiples of both 3 and 5. (Your algorithm adds these numbers to the total twice.)
Because 6 * 5 == 30 and 10 * 3 == 30, you're adding the some numbers up twice.
$sum = 0;
$i = 0;
foreach(range(0, 999) as $i) {
if($i % 3 == 0 || $i % 5 == 0) $sum += $i;
}
Because you double-count numbers that are multiple of both 3 and 5, i.e. multiples of 15.
You can account for this naively by subtracting all multiples of 15.
for ($j = 15; $j < $maxnumber; $j += 15)
{
$e += $j;
echo $j.',';
}
$total = $total - $d;
In your case, if it is 15, you will add the number twice.
Try this:
$t = 0;
$d = 0;
for ($i = 0; $i <= $maxnumber; $i++){
if ($i % 3 == 0)
$t+= $i;
else if ($i % 5 == 0)
$d += $i;
}
echo $t.'<br>'.$d;
I think that in your code, if a number is a multiple of 3 and 5, it is added twice. Take 15 for example. It's in your list of multiples of 3 and in the list of multiples of 5. Is this the behaviour you want?
One of the best approach to this solution (to achieve optimum time complexity), run an Arithmetic Progression series and find the number of terms in all series by using AP formula: T=a+(n-1)d, then find sum by : S=n/2[2*a+(n-1)d]
where : a=first term ,n=no. of term , d=common deference, T=nth term
The code solution below has been implemented to suit the question above - so the values 3 and 5 are hard-coded. However, the function can modified such that values are passed in as variable parameters.
function solution($number){
$val1 = 3;
$val2 = 5;
$common_term = $val1 * $val2;
$sum_of_terms1 = calculateSumofMulitples($val1,$number);
$sum_of_terms2 = calculateSumofMulitples($val2,$number);
$sum_of_cterms = calculateSumofMulitples($common_term,$number);
$final_result = $sum_of_terms1 + $sum_of_terms2 - $sum_of_cterms;
return $final_result;
}
function calculateSumofMulitples($val, $number)
{
//first, we begin by running an aithmetic prograssion for $val up to $number say N to get the no of terms [using T=a +(n-1)d]
$no_of_terms = (int) ($number / $val);
if($number % $val == 0) $no_of_terms = (int) ( ($number-1)/$val ); //since we are computing for a no of terms below N and not up to/exactly/up to N. if N divides val with no remainder, use no of terms = (N-1)/val
//second, we compute the sum of terms [using Sn = n/2[2a + (n-1)d]
$sum_of_terms = ($no_of_terms * 0.5) * ( (2*$val) + ($no_of_terms - 1) * $val );
// sum of multiples
return $sum_of_terms;
}
You can run a single loop checking whether the number is multiple of 3 OR 5:
for ($i = 0; $i < $maxnumber; $i++)
{
if($i%3 || $i%5){
$t += $i;
echo $i.',';}
}
I think the original code is not including numbers which are multiples of both 3 and 5 in the total: if the test for multiple of 3 matches, it takes that and goes on.
If you total the multiples of 15 up to 1000, you get 33165, which is exactly the difference between your total, 266333, and the original total, 233168.
Here's my solution to the question:
<?php
$sum = 0;
$arr = [];
for($i = 1; $i < 1000; $i++){
if((int)$i % 3 === 0 || (int)$i % 5 === 0)
{
$sum += $i;
array_push($arr,$i);
}
}
echo $sum;
echo '<br>';
print_r($arr);//Displays the values meeting the criteria as an array of values

Adding Ordinal Contractions to $i [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php display number with ordinal suffix
I'm attempting to add ordinal contractions i.e.(st/nd/rd/th) to an increment.
Somehow I need to get the last digit of $i to test it against my if statements...
Here is my code so far:
$i = 1;
while($i < 101 ){
if($i == 1){$o_c = "st";}else{$o_c = "th";}
if($i == 2){$o_c = "nd";}
if($i == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
You can use the modulus (%) operator to get the remainder when dividing by 10.
$i = 1;
while($i < 101 ){
$remainder = $i % 10;
if($remainder == 1){$o_c = "st";}else{$o_c = "th";}
if($remainder == 2){$o_c = "nd";}
if($remainder == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
What about using the modulus operator: $i % 10?
Display numbers with ordinal suffix in PHP
(that thread has other solutions. I liked that one)

Categories