I've this code and I don't understand the output. I was expecting an output 121.
I looking to understand this specific function and not the factorial function.
Code:
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
a($num - 1);
}
$totFac++;
return $totFac;
}
$result = a(5);
echo 'result: '.$result;
Output:
126
First of all, the factorial of 5 is 120 explained as:
5! = 5 x 4 x 3 x 2 x 1 = 120
Second, this is how a factorial should look like with recursive call:
function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
If you don't mean factorial, then this is how it happens if I alter your function
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
echo "calc totFac: " . $totFac . "\n";
a($num - 1);
}
$totFac++;
echo "increment totFac: " . $totFac . "\n";
return $totFac;
}
$result = a(5);
echo 'result: '.$result;
This is the output of the echo
calc totFac: 5
calc totFac: 20
calc totFac: 60
calc totFac: 120
calc totFac: 120
increment totFac: 120
increment totFac: 121
increment totFac: 122
increment totFac: 123
increment totFac: 124
increment totFac: 125
result: 126
<?php
function a($num) {
static $totFac = 1;
if($num > 0){
$totFac = $totFac * $num;
echo 'totfac: '.$totFac . "<br>";
return a($num - 1); //you have to return here to stop execution
} else {
$totFac++;
return $totFac;
}
}
$result = a(5);
echo 'result: '.$result;
?>
Look at the comment for explaination
Related
I have a variable that adds and returns numbers actually this numbers increases with time.
Now I want that if the numbers get to say 2000000 (2 millions) it should remove all the zeros and return 2m.
Any ideas how I can go about this ?
you can use the following function
function millionType($value){
return ($value / 1000000)."m";
}
echo millionType(1500000); // 1.5m
echo "<br/>";
echo millionType(2000000); // 2m
echo "<br/>";
echo millionType(2500000); // 2.5m
echo "<br/>";
echo millionType(3000000); // 3m
Edit: you can use this, you can change it to your desire type.
function millionType($value){
$million = (int)($value / 1000000);
$surplus = ($value % 1000000);
$myType = "";
if($million > 0)
$myType .= $million."m ";
$thousands = (int)($surplus / 1000);
if($thousands > 0)
$myType .= $thousands."k ";
$lastPart = ($surplus % 1000);
if($lastPart > 0)
$myType .= "and ".$lastPart;
echo $myType;
}
echo millionType(1500001); // 1m 500k and 1
echo "<br />";
echo millionType(1000001); // 1m and 1
echo "<br />";
echo millionType(10000540); // 10m and 540
Question is a duplicate of Shorten long numbers to K/M/B?
Neverthless here is my answer:
function shorten($number){
$suffix = ["", "K", "M", "B"];
$percision = 1;
for($i = 0; $i < count($suffix); $i++){
$divide = $number / pow(1000, $i);
if($divide < 1000){
return round($divide, $percision).$suffix[$i];
break;
}
}
}
echo shorten(1000);
I need to calculate if avg is > 7.75 means Heavy else No.
What the concern is that.
Need to calculate that from $x to $y range of all numbers comes int.
But i was able to get one Avg only not sure what to do to get all numbers Average values and store in Array comes in that range.
function heavyDecC ($x,$y)
{
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
return $result[] = array($i,$avg,$isHeavy);
}
}
Here is a simple solution
<?php
$results = heavyDecC(2685, 5875);
// Display results like this
foreach ($results as $id){
echo "--------------------------<br/>";
foreach($id as $key => $val){
echo $key . " - " . $val . "<br />";
}
}
function heavyDecC ($x,$y) {
for($i=$x; $i<=$y; $i++){
$num = $x;
$isHeavy = "No";
$num_length = strlen((string)$num);
$array = array_map('intval', str_split($num));
$sum = array_sum($array);
$average = ($sum / $num_length);
if($average > 7){
$isHeavy = "Yes";
}else {
$isHeavy = "No";
}
$newdata = array (
'Number' => $num,
'average' => $average,
'is_heavy' => $isHeavy
);
$md_array[$i]= $newdata;
$x++;
}
return $md_array;
}
?>
Results like this
--------------------------
Number - 2991
average - 5.25
is_heavy - No
--------------------------
Number - 2992
average - 5.5
is_heavy - No
--------------------------
Number - 2993
average - 5.75
is_heavy - No
--------------------------
Number - 2994
average - 6
is_heavy - No
--------------------------
Number - 2995
average - 6.25
is_heavy - No
--------------------------
Number - 2996
average - 6.5
is_heavy - No
--------------------------
Number - 2997
average - 6.75
is_heavy - No
--------------------------
Number - 2998
average - 7
is_heavy - No
--------------------------
Number - 2999
average - 7.25
is_heavy - Yes
--------------------------
You have return value in every loop so it will only get last index values ! So you need to return outside of the loop ....
function heavyDecC ($x,$y)
{
$result = array();
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
$result[] = array($i,$avg,$isHeavy);
}
return $result; // return here outside of loop
}
Move your return outside of the for loop, like this:
function heavyDecC ($x,$y)
{
$result = array();
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
$result[] = array($i,$avg,$isHeavy);
}
return $result;
}
I'd like to convert currency values like this:
4 000 000 000 => 4b
2 000 000 => 2m
2 250 000 => 2.25m
195 000 => 195k
10 000 => 10k
Anyone know of an existing library/function that does this, or do I need to write this myself by means of conditionals that cater for 0-9999, 10000-999999, and so on..?
You can create a custom function for this. The below one, firstly removes everything but numbers from string. Then it formats the number according how big is it and to the digit provided.
function shorten($num, $digits = 1) {
$num = preg_replace('/[^0-9]/','',$num);
if ($num >= 1000000000) {
$num = number_format(($num / 1000000000), $digits, '.', '') + 0;
$num = $num . "b";
}
if ($num >= 1000000) {
$num = number_format(($num / 1000000), $digits, '.', '') + 0;
$num = $num . 'm';
}
if ($num >= 1000) {
$num = number_format(($num / 1000), $digits, '.', '') + 0;
$num = $num . 'k';
}
return $num;
}
echo shorten("4 000 000 000");
echo shorten("3 200 000 000");
echo shorten("195 000");
Demo: http://codepad.org/K971MzVx
You can create a function like this to display shorten the values but it would miss the exact value
function shortifyCurrency($var){
$var=doubleval($var);
if($var>=1000000000){
$var=($var/1000000000)."b";
}else if($var>=1000000){
$var=($var/1000000)."m";
}else if($var>=1000){
$var=($var/1000)."k";
}
return $var;
}
//Output 4b
echo shortifyCurrency(4000000000);
Or you could use the following function to display more detailed information about the value
function shortifyCurrencyXtream($var){
$var=doubleval($var);
$print_str="";
if($var>=1000000000){
$print_str.=round($var/1000000000)."b";
$var=$var%1000000000;
}
if($var>=1000000){
if($print_str!="") $print_str.=" ";
$print_str.=round($var/1000000)."m";
$var=$var%1000000;
}
if($var>=1000){
if($print_str!="") $print_str.=" ";
$print_str.=($var/1000)."k";
}
return $print_str;
}
//Output 4b 101m 500.3k
echo shortifyCurrencyXtream(4100500300);
I want to know how to print factorial in php. I have try to search on Google but unable to find result with solid prove example (Php Code). I find this forloop, i'm not satisfy with this.
for($c=3; $c>=1;$c--){
for($d=$c; $d>=1; $d--){
echo $c;
}// for ends
echo "<br />";
}// for ends
Out Put 333 22 1
I want this printed as output:
5 * 4 * 3 * 2 * 1
For calculating factorial,
function factorial($in) {
return array_product(range(1, $in));
}
and use it like,
echo factorial(5);
If you want to print factorial,
function factorial_print($in) {
return implode(' * ', array_reverse(range(1, $in)));
}
and use it like,
echo factorial_print(5);
Calculating a factorial is pretty straightforward, and doesn't need recursion either:
function factorial($x) {
$r = 1;
for ($i = 2; $i <= $x; ++$i) {
$r *= $i;
}
return $r;
}
echo factorial(10), PHP_EOL;
Try this one
function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
Use following function inbuilt in PHP.
$fact1 = gmp_fact(5); // 5 * 4 * 3 * 2 * 1
echo gmp_strval($fact1) . "\n";
For more refer HERE
An example output of the following code may be this:
39 48 39 12 17 39 12
code:
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
...
...
echo "<video id='video$vidID'></video>";
}
}
I need it to output like this:
39 48 39_1 12 17 39_2 12_1
Notice how value 39 occurs three times and value 12 occurs twice.
After the first occurence of a value I need it
formatted like ##_#.
Can you help me code this up?
I appreciate your time and assistnace -
Derek
Use array_count_values like this:-
$array = array(1, 38, 1, 38,35);
print_r(array_count_values($array));
Output:-
Array
(
[1] => 2
[38] => 2
[35] => 1
)
Apply some logic to achieve what you want.
if (mysql_num_rows($filterResult)) {
$tmp = array();
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
if(isset($tmp[$vidID]) {
$tmp[$vidID] = $tmp[$vidID] + 1;
$vidID = $vidID . '_' . $tmp[$vidID];
} else {
$tmp[$vidID] = 0;
}
echo $vidID;
}
}
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$j=-1;
$vidID = $filterrow['routineID'];
$arr[]=$vidID;
for($i=0;$i<count($arr);$i++){
if($arr[$i]==$vidID)
$j++;
}
if($j==0)
$j="";
else $j="_".$j;
echo $vidID.$j;
}
}
Here is the code:
$array1 = array(39,48,39,12,17,39,12);
$array2 = array(39,48,39,12,17,39,12);
foreach($array1 as $value){
$counts1 = array_count_values($array1);
$counts2 = array_count_values($array2);
if(!empty($counts2[$value])){
if ($counts1[$value] > 1){
$count = $counts1[$value] - $counts2[$value] + 1;
if($count > 1) {
echo $value.'_'.($count - 1).' ';
} else {
echo $value.' ';
}
} else {
echo $value.' ';
}
array_shift($array2);
}
}
Here is the PHP fiddle