i have a number of minutes as ceiling and tasks that demand time in minutes, im doing a while loop to sum the time required by the tasks until reach the ceiling, but im having a problem with one step in the loop, the first number is the loop, the second the task required time, the third is my ceiling. I must reset the the task_required_time variable before or once it reach the ceiling. Using
if ($task_required_time > $ceiling) {task_required_time = 0}
it reset after its over the ceiling:
1 - 120 - 2400
2 - 304 - 2400
3 - 424 - 2400
4 - 2092 - 2400
5 - 2212 - 2400
6 - 3580 - 2400 <---- here is when it reset as expected but should be in the step before.
7 - 120 - 2400
8 - 330 - 2400
9 - 450 - 2400
10 - 570 - 2400
I will appreciate some light on it.
You need to make the compare at the begining of the code. this may help:
while ($rows = mysql_fetch_array($duedates)) {
if ($tiempo_para_mix + (($rows['tiempo_reproduccion'])*2)+($tiempo_tqc_final*60) > $tiempo_efectivo) {
$tiempo_para_mix = 0;
};
$tiempo_para_mix += (($rows['tiempo_reproduccion'])*2)+($tiempo_tqc_final*60);
echo $x . " - " . $tiempo_para_mix . " - " . $tiempo_efectivo . "</br>";
$x++;
}
Related
I have been creating a php script to analyse some data. At around 320 seconds the script just stops, no error messages.
I have created a separate simple php script to test and ensure its nothing in my code causing the problem. The simple php script does the same. I have set the max execution time to 25000 (again only for testing) and it still stops at 300 - 360 secs. What other factors maybe causing my script to timeout?
echo ini_get('max_execution_time');
for ($x = 0; $x <= 600; $x++) {
echo $x." - ";
sleep(1);
}
I would expect the script to continue running for 600 seconds, however its stops at 308.
OUTPUT:-
250000 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 ....... 299 - 300 - 301 - 302 - 303 - 304 - 305 - 306 - 307 - 308 -
Try this, you will have it continue working:
set_time_limit(0);
for ($x = 0; $x <= 600; $x++) {
ob_flush();
flush();
echo $x." - ";
sleep(1);
}
I am calculating the number that falls into a specific percentile. However, the problem with my equation is that if multiple elements in the array having the same value, this code will assign a different percentile number to each of those elements.
$percentile_50 = $array[round((50/100) * $count_array -.5)];
For example, first column is the percentile, and second is the score. You can see that same second gets different percentile, but in fact it should be the same.
How can I avoid this?
5-1
10-1
15-1
20-1
25-2
30-2
35-3
40-4
45-4
50-5
55-6
60-7
65-9
70-11
75-14
80-17
85-23
90-32
95-53
To calculate the kth percentile, you should :
Order all the values in the data set from smallest to largest.
Multiply k percent by the total number of values, n.
This number is called the index.
If the index obtained in Step 2 is not a whole number, round it up to the nearest whole number and go to Step 4. If the index obtained in Step 2 is a whole number, go to Step 5.
Count the values in your data set from left to right (from the smallest to the largest value) until you reach the number indicated by Step 3.
The corresponding value in your data set is the kth percentile
Count the values in your data set from left to right until you reach the number indicated by Step 2.
The kth percentile is the average of that corresponding value in your data set and the value that directly follows it.
( from here)
Translated to PHP:
$sData = sort($data);
$percentile = 50/100;
$index = ceil($percentile * count($sData));
$value = $sData[$index-1];
Or shorter:
$sData = sort($data);
$value = $sData[ceil((50/100) * count($sData)) - 1]
However, in a small dataset, sometimes the percentiles end up the same. For example, the following code produces the following output.
Code
$data = array(74, 15, 25, 62, 45, 9, 16, 63, 60, 76, 7, 20, 67, 30, 12);
sort($data);
for($i=5;$i<100;$i+=5){
echo $i . " - " . $data[ceil(($i/100) * count($data)) - 1];
echo PHP_EOL;
}
Output
5 - 7
10 - 9
15 - 12
20 - 12 //same as aboce
25 - 15
30 - 16
35 - 20
40 - 20 //same as above
45 - 25
50 - 30
55 - 45
60 - 45 //same as above
65 - 60
70 - 62
75 - 63
80 - 63 //same as above
85 - 67
90 - 74
95 - 76
we have to make a web game for a school project.
but im currently stuck at one point. its a mafia styled web game, you probably know one of these. When your charachter goes to the hospital to get his wounds fixed up he needs to pay a certain amount of money. this is calculated by the following code
$maxHeal = 100 - $health;
$costs =round(pow($maxHeal,1.8));
health is a number between 0 and 100 and the costs are based on exponential growth. but if the player can only afford 50 but types in 100, how do I make sure he only getw 50, and how do I make sure it are the first 50 health points, the most expensive ones and not the cheap ones, this will cause player to just type in 1 press enter to get some cheap health.
I hope my porblem is clear, if you have any questions about other parts of the codes please ask
thanks in advance
edit: to give some extra clearance,
when I am at 10 health(hp) and I want to go back to 100hp I need to get a 90 extra. there is a form where I can type how much hp I want to cure, so I type in 90 and the system requests to ad 90 to my life so it makes 100. to do this I need to check if the players can afford to pay for those 90 points. if I can not pay for 90 but can pay for 50 I want those 50 to be added anyways. but if I count from 1 to 50 and to one form 40(the remaining I need to heal another timer) it wil cost less than counting from 1 to 90 because of the exponential growth.
so I need 2 checks. I have to cure al I can afford, so if I can afford just 50 of the 90 hp I need, I will only get 50 points and pay for 50, but as this wil be cheaper how can I make sure that I pay for the 50 like I woulld pay for 90. so 50 and 40 need to be equal to one times 90
Based on your question (which is not totally clear to me, but hey, I'm in a good mood), I built the following example:
//total amount of health points
$points = 20000;
//health left
$health = 10;
//how many health do we miss? 100 = maximal
$maxHeal = 100 - $health;
//counter
$i = 0;
while($points > $cost = round(pow($maxHeal-$i,1.8))) {
//check if the user has enough points
if ($points - $cost > 0) {
$health++;
echo "Healt +1 point, total: " . $health . " (points " . $points . " - cost " . $cost . " = " . ($points - $cost) . " left)" . "\n";
} else {
echo "Can't heal anymore, not enough points left (" . $points . ")" . "\n";
}
$points -= $cost;
$i++;
}
echo "\n";
echo "Remaining points: " . $points . ", needs points for next repair: " . $cost;
With the following output:
Health now: 10
Healt +1 point, total: 11 (points 20000 - cost 3293 = 16707 left)
Healt +1 point, total: 12 (points 16707 - cost 3228 = 13479 left)
Healt +1 point, total: 13 (points 13479 - cost 3163 = 10316 left)
Healt +1 point, total: 14 (points 10316 - cost 3098 = 7218 left)
Healt +1 point, total: 15 (points 7218 - cost 3035 = 4183 left)
Healt +1 point, total: 16 (points 4183 - cost 2971 = 1212 left)
Remaining points: 1212, needs points for next repair: 2909
I hope this gets you going :)
Thank you for any help you can provide on this.
If we have:
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
For instance if:
$c_xp = 1200
Then:
$level = 4
Then I am asking if:
$level = 4
How much xp to get to level 5?
How can I get the amount of xp required to achieve the next level?
The formula to get the minimum XP required to reach a level $level is given by:
$minXP = 12.5 * (pow((2 * $level + 3), 2) - 41);
So you can set $level = 5 and $level = 4 and find the difference between them.
Analyse:
I ran a script to view the XP needed for each level:
$current_level = 0;
for($c_xp = 1;$c_xp < 7000;$c_xp++){
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
if($level > $current_level){
$current_level++;
echo "c_xp $c_xp level $level<br>";
}
}
The output is:
c_xp 1 level 1
c_xp 100 level 2
c_xp 500 level 3
c_xp 1000 level 4
c_xp 1600 level 5
c_xp 2300 level 6
c_xp 3100 level 7
c_xp 4000 level 8
c_xp 5000 level 9
c_xp 6100 level 10
We can see that level 1 is an exception, then the difference of XP from level 2 to level 3 is 400, from level 3 to level 4: 500 and level 4 to level 5: 600 and so on...
The pattern is now clear,
Level 1 -> 1 XP
Level 2 -> Start a 100 XP
Level 3 -> Increase by 400 (500 XP)
Level 4 -> Increase by (400 + 100) 1000XP
Level 5 to Level X -> Keep increasing by 100
The code:
With the above analyse, I could come up with a loop in a function:
$c_xp = 1200;
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1;
echo 'Next level: '.current_level_xp($level+1).'<br>';
echo 'XP needed for next Level: '. (current_level_xp($level+1) - $c_xp);
function current_level_xp($current_level){
if($current_level <= 1){
return(1);
}else{
$increase = 400;
$needed = 100;
for($i=0;$i<$current_level-2;$i++){
$needed += $increase;
$increase += 100;
}
return $needed;
}
}
Output:
Next level: 1600
XP needed for next Level: 400
Use a math / formular plotting tool of your choosing to get an idea of the results of the used equation (which presumably isn't yours):
Now, there's probably even a way to reverse your formula for translating y into x. But I'm unsure if there's a floor reverse.
My testcase as follows:
echo crypt('string', '_....salt');//error
echo crypt('string', '_A...salt');//fast
echo crypt('string', '_AAAAsalt');//slow
Explanation as stated at http://www.php.net/manual/en/function.crypt.php:
CRYPT_EXT_DES - Extended DES-based hash. The "salt" is a 9-character
string consisting of an underscore followed by 4 bytes of iteration
count and 4 bytes of salt. These are encoded as printable characters,
6 bits per character, least significant character first. The values 0
to 63 are encoded as "./0-9A-Za-z". Using invalid characters in the
salt will cause crypt() to fail.
A dot is a printable character so why does it return an error? And which "order" applies on the used characters resulting "AAAA" more iterations than "A..."?
It says all in the quoted paragraph:
- least significant character first
- The values 0 to 63 are encoded as "./0-9A-Za-z"
So in your example "_....salt" would mean 0 rounds which obviously can't work.
and "_A...salt" is less than "_AAAAsalt" considering the least significant character comes first.
"_...Asalt" would also be more than "_A...salt"
This question is a bit old, however i found this when trying to wrap my head around how to create a hashing class for internal use here, and i came up with this little function which will base64 encode an integer with the appropriate characters/significance to be used as the 4 character 'iteration count'. Possible values are from 1 to 16,777,215
private function base64_int_encode($num){
$alphabet_raw = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$alphabet = str_split($alphabet_raw);
$arr = array();
$base = sizeof($alphabet);
while($num){
$rem = $num % $base;
$num = (int)($num / $base);
$arr[]=$alphabet[$rem];
}
$arr = array_reverse($arr);
$string = implode($arr);
return str_pad($string, 4, '.', STR_PAD_LEFT);
}
Hope it helps someone!
The code of Klathmon is nice but has some mistakes:
First - alphabet
It is:
./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Should be:
./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Second - order of characters/digits
It generates for example: ...z
But it should generate: z...
The improved code:
function base64_int_encode($num) {
$alphabet_raw='./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$alphabet=str_split($alphabet_raw);
$arr=array();
$base=sizeof($alphabet);
while($num) {
$rem=$num % $base;
$num=(int)($num / $base);
$arr[]=$alphabet[$rem];
}
$string=implode($arr);
return str_pad($string, 4, '.', STR_PAD_RIGHT);
}
A number system used in Extended DES:
.... - 0 (Extended DES error)
/... - 1
0... - 2
1... - 3
2... - 4
3... - 5
4... - 6
5... - 7
6... - 8
7... - 9
8... - 10
z... - 63
./.. - 64
//.. - 65
0/.. - 66
1/.. - 67
Y/.. - 100
61.. - 200
g2.. - 300
E4.. - 400
o5.. - 500
M7.. - 600
w8.. - 700
UA.. - 800
2C.. - 900
cD.. - 1000
zz.. - 4095
../. - 4096
/./. - 4097
0./. - 4098
1./. - 4099
xzzz - 16 777 213
yzzz - 16 777 214
zzzz - 16 777 215
And in connection with salt:
_/...salt - 1
_0...salt - 2
_1...salt - 3
_2...salt - 4
_3...salt - 5
_4...salt - 6
_5...salt - 7
_6...salt - 8
_7...salt - 9
_8...salt - 10
_z...salt - 63
_./..salt - 64
_//..salt - 65
_0/..salt - 66
_1/..salt - 67
_Y/..salt - 100
_61..salt - 200
_g2..salt - 300
_E4..salt - 400
_o5..salt - 500
_M7..salt - 600
_w8..salt - 700
_UA..salt - 800
_2C..salt - 900
_cD..salt - 1000
_zz..salt - 4095
_../.salt - 4096
_/./.salt - 4097
_0./.salt - 4098
_1./.salt - 4099
_xzzzsalt - 16 777 213
_yzzzsalt - 16 777 214
_zzzzsalt - 16 777 215