Round down to nearest multiple of 10000 using PHP - php
If I have the following code which grabs an array of values and adds them all together, how can I then round them down to the nearest 10000 using PHP?
Here's the code I currently have
$rows = $db->get("sales");
$sales = 0;
foreach($rows as $row) {
$stock = $sales + $row['sales'];
}
return $sales;
An example result would be
146740
How could I then make that returned as
140000
Although if I had a number greater than 1 million, how could I have that returned as just 1 million?
Divide by 10000, use floor to round down to an integer, then multiply by 10000:
$x = 146740;
$x = 10000 * floor($x/10000);
Or subtract the remainer:
$x = 146740;
$x = $x - ($x % 10000);
To extend this to 1 million, you can do:
if ($x > 1000000) {
$divisor = 1000000;
} elseif ($x > 10000) {
$divisor = 10000;
} else {
$divisor = 1;
}
$x = $x - ($x % divisor);
you could divide the value by 1000. If it is integer 146740/1000 = 146. And after that multiply by 1000 will give 146000
Related
PHP random numbers frequency of occurrence
In PHP I want to generate random numbers from 1 to 10 in a loop. So for example: $factor="1"; // this will be changed in another area for ($i=0;$i<10;$i++) { if ($factor=="1") {$output = rand(1,10);} else if ($factor=="2") {$output = rand(1,10);} else {$output = rand(1,10);} } Now to explain this - In result I want to receive 10 random numbers, but when $factor = "2", in that case I want to receive numbers from 6 to 10 more frequently as lower numbers. It means, from 10 numbers I need to have 80% higher random numbers (it means larger than 5) and in 20% lower numbers (5 or lower). E.g. 1,2,6,7,8,9,7,9,8,6 (1,2 are the only lower numbers = 20%, the rest are higher = 80) If the $factor will change, then I want to change the percentage, in that case for example 40% lower numbers, 60% higher numbers. The idea I have is to put each output in the loop to an array, then check each result and somehow calculate, if there is no 80% of larger numbers, then get random numbers again for those, but this seems to be an overkill. Is there a simplier solution?
Let's go with the percentages you mention and first generate a random number between 1 and 100. Then the lower number, 1 to 20, have to represent outputs 1 to 5 and the higher numbers, 21 to 100, have to represent output 6 to 10. In PHP that would look like this: function HighMoreOften() { $percentage = rand(1, 100); if ($percentage <= 20) { return rand(1, 5); } else { return rand(6, 10); } } That should do the trick. You can also convert the percentage you got into the output, this would probably be slightly faster: function HighMoreOften() { $percentage = rand(1, 100); if ($percentage <= 20) { return ceil($percentage / 5); } else { return 6 + ceil(($percentage - 20) / 20); } } but personally I think the first version is easier to understand and change.
To change frequency you gonna need an array of numbers. And a sum to this direction. frequency is the relation of something between an array of things. $t = 0; // $factor = 1; // let's say that this means 20% $factor = 2; // let's say that this means 40% if ($factor === 1) { for ($i = 1; $i <= 80; $i++) { $t += rand(1,10); } for ($i = 1; $i <= 20; $i++) { $t += rand(6,10); } } else if ($factor === 2) { for ($i = 1; $i <= 60; $i++) { $t += rand(1,10); } for ($i = 1; $i <= 40; $i++) { $t += rand(6,10); } } else { for ($i = 1; $i <= 100; $i++) { $t += rand(1,10); } } echo round(($t/100), 0); Something like that! :)
I came with a very simple (maybe creepy) solution, but this works as I wanted: echo print_r(generate("2")); function generate($factor) { $nums=array(); for ($i=0;$i<10;$i++) { if ($i<$factor) {$rnd = rand(1,5);} else {$rnd = rand(6,10);} array_push($nums,$rnd); } return $nums; } I can also shuffle the final array results, as the lower numbers will be on the beginning always, but in my case it doesn't matter.
PHP generate cent value of entered amount
In my database I store the amount of a user in cents. Now I need to convert user input to integer. input of user(string) :|output (int): 1.00 100 1 100 1,51 151 I have no idea, how to solve this. Formatting amounts like: 1.00 and 1,00 isn't a big problem, but what's the best way to format the amount like "1" to the right amount of cents? What I use at the moment (not working with inputs like "1"): $value = intval(str_replace([',','.'],'', $request->amount));
What about casting as a float and multiplying by 100? <?php header("Content-type: text/plain"); $x = '1,51'; $x = (float) str_replace(',','.', $x); echo (int) ($x * 100)."\n"; $x = '1'; $x = (float) str_replace(',','.', $x); echo (int) ($x * 100)."\n"; $x = '1.51'; $x = (float) str_replace(',','.', $x); echo (int) ($x * 100)."\n"; $x = '1,00'; $x = (float) str_replace(',','.', $x); echo (int) ($x * 100)."\n"; $x = '1.00'; $x = (float) str_replace(',','.', $x); echo (int) ($x * 100)."\n"; returns: 151 100 151 100 100
str_replace should be used with two arrays or two strings $value = intval(str_replace([',','.'],['',''], $request->amount));
How to generate random numbers to produce a non-standard distributionin PHP
I've searched through a number of similar questions, but unfortunately I haven't been able to find an answer to this problem. I hope someone can point me in the right direction. I need to come up with a PHP function which will produce a random number within a set range and mean. The range, in my case, will always be 1 to 100. The mean could be anything within the range. For example... r = f(x) where... r = the resulting random number x = the mean ...running this function in a loop should produce random values where the average of the resulting values should be very close to x. (The more times we loop the closer we get to x) Running the function in a loop, assuming x = 10, should produce a curve similar to this: + + + + + + + + + Where the curve starts at 1, peeks at 10, and ends at 100. Unfortunately, I'm not well versed in statistics. Perhaps someone can help me word this problem correctly to find a solution?
interesting question. I'll sum it up: We need a funcion f(x) f returns an integer if we run f a million times the average of the integer is x(or very close at least) I am sure there are several approaches, but this uses the binomial distribution: http://en.wikipedia.org/wiki/Binomial_distribution Here is the code: function f($x){ $min = 0; $max = 100; $curve = 1.1; $mean = $x; $precision = 5; //higher is more precise but slower $dist = array(); $lastval = $precision; $belowsize = $mean-$min; $abovesize = $max-$mean; $belowfactor = pow(pow($curve,50),1/$belowsize); $left = 0; for($i = $min; $i< $mean; $i++){ $dist[$i] = round($lastval*$belowfactor); $lastval = $lastval*$belowfactor; $left += $dist[$i]; } $dist[$mean] = round($lastval*$belowfactor); $abovefactor = pow($left,1/$abovesize); for($i = $mean+1; $i <= $max; $i++){ $dist[$i] = round($left-$left/$abovefactor); $left = $left/$abovefactor; } $map = array(); foreach ($dist as $int => $quantity) { for ($x = 0; $x < $quantity; $x++) { $map[] = $int; } } shuffle($map); return current($map); } You can test it out like this(worked for me): $results = array(); for($i = 0;$i<100;$i++){ $results[] = f(20); } $average = array_sum($results) / count($results); echo $average; It gives a distribution curve that looks like this:
I'm not sure if I got what you mean, even if I didn't this is still a pretty neat snippet: <?php function array_avg($array) { // Returns the average (mean) of the numbers in an array return array_sum($array)/count($array); } function randomFromMean($x, $min = 1, $max = 100, $leniency = 3) { /* $x The number that you want to get close to $min The minimum number in the range $max Self-explanatory $leniency How far off of $x can the result be */ $res = [mt_rand($min,$max)]; while (true) { $res_avg = array_avg($res); if ($res_avg >= ($x - $leniency) && $res_avg <= ($x + $leniency)) { return $res; break; } else if ($res_avg > $x && $res_avg < $max) { array_push($res,mt_rand($min, $x)); } else if ($res_avg > $min && $res_avg < $x) { array_push($res, mt_rand($x,$max)); } } } $res = randomFromMean(22); // This function returns an array of random numbers that have a mean close to the first param. ?> If you then var_dump($res), You get something like this: array (size=4) 0 => int 18 1 => int 54 2 => int 22 3 => int 4 EDIT: Using a low value for $leniency (like 1 or 2) will result in huge arrays, since testing, I recommend a leniency of around 3.
Dividing a integer equally in X parts
I'm looking for a efficient way in PHP to divide a number in equal part. Number will always be integer (no float). Let's say that I have an array $hours with values from "1" to "24" ($hours['1'], etc) and a variable $int containing an integer. What I want to acheive is spreading the value of $int equally in 24 parts so I can assing the value to each corresponding array entries. (Should the number be odd, the remaining would be added to the last or first values in the 24). Regards,
Here's the algorithm you're looking for; it evenly spreads an integer N over K cells: for i = 0 to K array[i] = N / K # integer division # divide up the remainder for i = 0 to N mod K array[i] += 1
Try this code <?php $num = 400; $val = floor($num/24); for($i=0;$i<24;$i++) { $arr[$i] = $val; } $arr[0] += $num - array_sum($arr); ?>
function split($x, $n) { // If we cannot split the // number into exactly 'N' parts if($x < $n) echo (-1); // If x % n == 0 then the minimum // difference is 0 and all // numbers are x / n else if ($x % $n == 0) { for($i = 0; $i < $n; $i++) { echo ($x / $n); echo (" "); } } else { // upto n-(x % n) the values // will be x / n // after that the values // will be x / n + 1 $zp = $n - ($x % $n); $pp = $x / $n; for ($i = 0; $i < $n; $i++) { if($i >= $zp) { echo (int)$pp + 1; echo (" "); } else { echo (int)$pp; echo (" "); } } } } // Driver code $x = 5; $n = 3; split( $x, $n);
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