Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to solve this equation in php:
Ym = Zm * Zm+1 * Zm+2...Zm+n
Where,
Y = Unknown
m = array if numbers (1,2,3,4...)
z = array of numbers (8,6,9,10...). Number of elements of "Z" and "Y" array dependent from array "m".
For instance:
m = (1,2,3,4);
z = (5,6,10,7);
Y1=5*6*10*7
Y2=6*10*7
Y3=10*7
Y4=7
This equation was written by me. I think I've done something wrong while writing equation (Ym = Zm * Zm+1 * Zm+2...Zm+n). What I need is the example above.
Thanks!
Something in the lines of:
$m = array(0, 1, 2, 3);
$z = array(5, 6, 10, 7);
$Y = array(1, 1, 1, 1);
for ($x=0; $x<count($m); $x++) {
for ($w=$x; $w<count($m); $w++) {
$Y[$m[$x]]=$Y[$m[$x]]*$z[$w];
}
print "Y[{$m[$x]}]={$Y[$m[$x]]}\n\r";
}
Example http://sandbox.onlinephpfunctions.com/code/063e363111f4da275475c9c39dd8e7281083d22b
In functional programming language like Mathematia aka Wolfram language this would just be:
Y=FoldList[Times,1,{5,6,10,7}]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi am having 10 elements in array . Am trying to get my random element mostly from first 5 elements. Which means random element appearance from first 5 elements should be much greater than next 5 elements
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = $arr[array_rand($arr)];
Am using above one to get the random element normally
Use function rand(min_num, max_num):
function rand5($array) {
$part = rand(1, 10);
return ($part > 3) ? $array[rand(0, 4)] : $array[rand(5, 9)];
}
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = rand5($arr);
Try this simple and easy code :-
$arr = array('a','b','c','d','e','f','g','h','i','j');
$rand = rand(0,9);
echo $arr[($rand <= 6 ? ($rand%5) : $rand)];
You just have to get the random number between 0 to 9 and divide that number in 70%(0-6) and 30%(7-9). If its greater than 5 then only use the remainder else directly get that number
Here is the fiddle :- https://3v4l.org/TWGJJ
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
These are my strings
2012-13
2013-14
2014-15
2015-16
2016-17
2017-18
I my string is 2014-15 as current financial year, I want my next my next financial year as 2015-16
How can I achieve this with PHP? I get these year list form mysql table, but I want next financial year in php
Try this:
$financial_year = "2012-13";
$fin_array = explode("-", $financial_year);
$next_fin_array[0] = $fin_array[0] + 1;
$next_fin_array[1] = $fin_array[1] + 1;
$next_financial_year = implode("-", $next_fin_array); // Gives 2013-14
Hope this helps.
$myStr = '2014-15';
$yr1 = (int) substr($myStr, 0, 4);
$yr2 = (int) substr($myStr, -2);
echo ($yr1+1).'-'.($yr2+1);
// 2015-16
$financeYear = DateTime::createFromFormat ( "Y-d" ,"2014-15");
$financeYear->modify('+ 1 year');
echo $financeYear->format("Y-d");
Result
2015-15
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Input: any number in 1-15 or 64-79 range which is a sum of either (1, 2, 4, 8, 64) in any combination
Output: an array of integers from this list: (1, 2, 4, 8, 64) the sum of which equals the input number.
e.g.
input 72, output array(8, 64)
input 13, output array(1,
4, 8)
Since you have not included your code in the question, no one can help you with your code. But here is a general approach without any code that should work for this problem.
Start with your input number and an empty array to hold the sum elements. Iterate over your array of addends in descending order, appending each one to your sum array and subtracting it from the input number until the input number reaches zero.
mkasberg provided the solution:
$in = 72;
$out = array();
$a = array_reverse(str_split((string)decbin($in)));
foreach($a as $k => $v){
if ($v != "0") array_push($out, pow(2,$k));
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this a record of: 10/2 * 100%
This is equal to 5
How does it save in PHP?
This not work:
($int1/$int2) * 100
Assuming $int1 = 10 and $int2 = 2, you'd have this sum;
echo (10 / 2) * 1; //1 being 100%
Which outputs 5.
http://codepad.org/twp2TduF
There is nothing wrong - it gives your desired result. I assume you were * 100, which would give 500.
Other percentages
* 0.25 = 25%
* 0.75 = 75%
..and so-on
Or refer to this answer
You cannot use percentage this way.
You will need to divide them by 100:
<?php
$int1 = 3;
$int2 = 6;
echo ($int1/$int2) * 100/100;
this above is 100%
and this:
echo ($int1/$int2) * 25/100;
is 25%