7001 write 8? round(7001/1000) - php

I have to write numbers in groups of 1000.
so, if I have 7000, I want to write:
1 2 3 4 5 6 7
but If I have 7001 or more, I want to write:
1 2 3 4 5 6 7 8
php
$num = 22501;
$num = round($num/1000);
for ($i=1; $i<=$num; $i++){
echo $i;
}
if my number is 22501 I will write 1 to 23. but if it is 22001 it will write 1 to 22.
I want to write 1 to 23. how can I do this?

Related

I want to print numbers pattern program but it not give me wrong out put

I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP

PHP - Why floor() give me another result of sams calculation [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
for($i=0;$i<=15120;$i+=504){
echo ($i/60/8.4) . " - " .floor($i/60/8.4)."<br>";
}
Result (i make ** at problem) :
0 - 0
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 6
8 - 8
9 - 8
10 - 10
11 - 11
12 - 12
13 - 13
14 - 13
15 - 15
16 - 16
17 - 17
18 - 17
etc...
At start i think i apply "Floo" at all calculation but (for line "7-6") also $i = 3528 :
3528 / 60 = 58,8 == Floor ==> 58 / 8 = 7.25
floor of 7.25 =/= 6
it because of PHPs floating point precision
so your 8th calculation for example will not return 7, but something like 6.9999999999999999999999999999
as this is a double, it will be rounded to 7 on output, try this out:
$x = 6.9999999999999999999999999999999999999999999999999999999;
echo $x;
when you use floor() it will (correctly) round it down to 6

Undesired new lines added to text document when writing to it

I was just trying to do a simple sorting algorithm on a matrix that I read from a matrix.txt file and append the sorted matrix back to the file.
The problem is that undesired new lines are written to the text file. I also tried in parallel to echo the same things I am writing in the text file, but the echo prints everything okay.
// .. reading the file and sorting the matrix ..
// Write the sorted matrix back to the text file
$handle = #fopen("matrix.txt", "a");
if ($handle) {
fwrite($handle, PHP_EOL . PHP_EOL . "Sorted matrix:" . PHP_EOL);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
echo $matrix[$i][$j] . " ";
fwrite($handle, $matrix[$i][$j] . " ");
}
fwrite($handle, PHP_EOL);
echo "<br>";
}
fclose($handle);
}
matrix.txt file contents:
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
1 2 5 2 5 8 12 323 1 4
8 32 2 1 3 82 2 8 4 2
In the web browser it echoes the matrix nicely sorted, each row by itself; however, in the text file, the following is appended:
Matrix sorted using selection sort:
1 1 2 2 4
5 5 8 12 323
1 2 2 2
3 4 8 8 32 82
1 1 2 2 4
5 5 8 12 323
1 2 2 2 3 4 8 8 32 82
Any clues what could cause this? Thanks in advance !
The problem isn't in the code you posted; it's in the input matrix you provided. Notice that every extra newline corresponds to the item which used to be at the end of the row, except for the last row. That's because the final newline from each row is being included when you read the line, and explode (which I imagine you're using) doesn't know to remove it. You could simply trim the lines before exploding to fix this, or specifically remove \r and \n characters.

proper arrangement in foreach

$random = rand(4, 23);
$range = range(1, $random );
HI.. guys
I have a random range value here in foreach function i want to display with below
rules.. my aim is to display like a square box
if i get range 1 to 3 it has to display table like this
1 2
3
if range from 1 to 6
1 2 3
4 5 6
if range from 1 to 19
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19
get the ceil of the square root of the number of records, and then anytime you're at an index with a mod of that value that is equal to 0, start a new line. Since you already have $random something like:
$dim = ceil(sqrt($random));
foreach ($range as $index => $number) {
print $number;
if (!(($index + 1) % $dim)) {
print "\n";
}
else {
print " ";
}
}
May need some adjustment (I'm not in PHP mode atm) and also doesn't factor in the padding but that should be straightforward.

PHP Weighted random number

How can I generate a weighted random number between 1 and 10 with 10 being the highest chance and 1 being the lowest chance?
rand(1,10) ?
Needs to be a simple one line code since it will be run 100,000's of times
OK I think I understand what you're trying to say..
try this :
mt_rand(mt_rand(1, 10),10 );
I looped it a million times :
10 = 292634
9 = 193333
8 = 142815
7 = 109580
6 = 84616
5 = 64498
4 = 47666
3 = 33450
2 = 21286
1 = 10122

Categories