diviside numbers using php [closed] - php

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 9 years ago.
Improve this question
I am trying to create a php code to execute a list of divisions, i have tried to put a code together but being a novice, it's not quite working.
<?php
$division(6,true);
for ($i = 1; $i <= 6; $i++) {
if(($division / $i) == $result)
{echo "<p>$division &division; $i = $result</p>";}
}
?>
$division is meant to be the main number, $division will get divided by every number upto and including $division.
$i should list a string of number to from 1 to 6 in this case.
then $divide ÷ $i = $result.
I am hoping for this to print out the list as shown below.
6 ÷ 1 = 6
6 ÷ 2 = 3
6 ÷ 3 = 2
6 ÷ 4 = 1.5
6 ÷ 5 = 1.2

try this...? I'm not sure what the IF statement is trying to accomplish, though
<?php
division(6);
function division($num)
{
for ($i = 1; $i < $num; $i++)
{
$result = $num / $i;
//if(($division / $i) == $result)
//{
echo "$num / $i = $result<br/>";
//}
}
}
?>
output:
6 / 1 = 6
6 / 2 = 3
6 / 3 = 2
6 / 4 = 1.5
6 / 5 = 1.2
edit: please note that % is usually the modulus operator in programming.

Related

Check if password contains easy sequences with php [closed]

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 2 years ago.
Improve this question
I want to have an php check for easy passwords patterns like
123456
123456789
abcde
98764321
101010
202020
Is it possible to have a check like this that doesn't rely in maintaining an array of pre-defined strings ?
Just iterate to each letter and check if the next one is same, one less or one more as current. So you can create a score of uniqueness.
For last case you can check if there are duplicate characters and remove from score, too.
$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];
foreach ($passwords as $password) {
$score = $length = strlen($password);
$chars = str_split($password);
for ($i = 0; $i < $length - 1; $i++) {
$currentChar = $chars[$i];
$nextChar = $chars[$i + 1];
if ($currentChar === $nextChar
|| ord($currentChar) - 1 === ord($nextChar)
|| ord($currentChar) + 1 === ord($nextChar)
) {
$score--;
continue;
}
}
$unique = array_unique($chars);
$score -= $length - count($unique);
echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;
}
123456: 1 / 6 Bad
123456789: 1 / 9 Bad
abcde: 1 / 5 Bad
98764321: 2 / 8 Bad
101010: -3 / 6 Bad
202020: 2 / 6 Bad
xhdgszu: 7 / 7 Good

The random function ranges from 1> 10, generating the number required [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a random function between 1 and 100
I want it to random out 2 < x <10, x is the number I need, if x does not randomize the number in that range then continue random until satisfactory, how to write
Thank.
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }
Just use a simple while loop:
$x = rand(1,100);
while (2 > $x || $x > 10) {
$x = rand(1,100);
}
echo $x;
The above loop will only stop if your number is between your specified range (not sure if I understood your needs correctly).
Another approach could be to simply use a different interval for the rand() function.
EDIT: Please check the comments of your question, there is a solution.
<?php
for (
$i = 0, $limit = 10;
$i < $limit && $x = rand(1,100);
$i++
) {
echo $x, ' ', (2 < $x && $x < 10) ? 'HIT' : 'MISS', "\n";
}
Example output:
76 MISS
83 MISS
59 MISS
13 MISS
7 HIT
18 MISS
9 HIT
39 MISS
79 MISS
91 MISS

How to program loop with two different variables [closed]

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 3 years ago.
Improve this question
Imagine you want to add the sum of 2 dice so the output looks like this in 6 lines:
2 3 4 5 6 7 3 4 5 6 7 8 9 4 5 6 7 8 9 10 11 5 6 ... 7 8... 8 9...
By only programming a single loop ?
I tried to do it with nested loops but could figure out the logic for it.
You can do with one while loop and two variables for two dices.
<?php
$x = 1;
$y = 1;
while ( $x <= 6 ) {
echo $x + $y;
$y++;
if ( $y > 6 ) {
$y = 1;
$x++;
}
}
It's essentially a mechanical counter. When the first wheel completes a rotation, it resets, and advances the second wheel. Except in this case they are dice. When $y is greater than six, it resets and advances $x.
You would use similar code when dealing with dates, advancing months when the days tick over etc.
The code would be simpler using two loops.
for ( $x = 1; $x <= 6; $x++ ) {
for ( $y = 1; $y <= 6; $y++ ) {
echo $x + $y;
}
}
As this was not allowed, the while loop is the 'first wheel' and the if statement determines when to reset and advance the second.
https://www.hackerrank.com/ is good if you want to practice solving coding problems

Euler Project 14 - Longest Collatz sequence in PHP [closed]

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
This is my solution to Euler Project Problem 14
<?php
$count = 0 ;
$max = 0;
for($n = 2 ; $n < 1000000 ; $n++){
while ($n > 1)
{
if ($n % 2 == 0 )
{
$n = $n/2;
}
else
{
$n = 3*$n + 1 ;
}
$count += 1;
if($count > $max )
{
$max = $count;
$final = $n;
}
}
}
echo $final;
>?
It took so long to run.I looked some other solutions and they were very similar to my code logically,but they were running way too faster than mine.
My question is,what is it that makes my code inefficient? What am I missing here?
Thanks ^^
Your approach is straight forward and unpolished.
You can use dynamic programming in order to improve the runtime (complexity approach).
Let's say you want to compute for 5. You will have :
5 -> 16 -> 8 -> 4 -> 2 -> 1.
But if you do that you will also have computed the value for 8 and 16.
The idea is to store the value that you have already computed in order to save work when you will need them later.
Working on the complexity of an algorithm will be the key of some problems, so better get used to it early.
An other reason why it is slow, is the choice of the language. For example try with C it will run a lot faster.

Divide integer number into 3 parts using php [closed]

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 9 years ago.
Improve this question
I need to divide an integer value into 3 parts using php
for eg: I have a value 9 the answer is like 3 + 3 + 3
if i have value 10 the ans like 3 + 3 + 4 or something like that
You can use a modulus function.
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
Output:
The numbers are 3 + 3 + 4.
If you have a value that is >= 3 you can do the following:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);

Categories