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

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.

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

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

PHP: Is there a difference between use of ++ or +=1 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.
But what about in PHP?
Is there any difference between the += and ++ or does it just not really matter?
Well whatever you might say about conventions, try running the following...
$i = 1; $s = 's';
$i++; $s++;
echo $i.'<br>'.$s.'<br>';
$i = 1; $s = 's';
$i += 1; $s += 1;
echo $i.'<br>'.$s.'<br>';
the output is somewhat unexpected...
2
t
2
1
so I would say it could matter very much which is chosen!
x += 1 is rather equivalent to ++x.
All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.

how to use biasedNumberBetween faker? [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 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)

Bold a number from where first instance of number above 0 is found - 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 8 years ago.
Improve this question
I have long decimal numbers that I'd like to make easier to read. There's many 0's involved and I would like to bold the end of the string from where the numbers stop being just 0's. The number will always have 8 decimal places, no more, no less. It's worth noting that I would like any trailing zeros to also be bold, I don't just want to avoid bolding 0 as a whole.
Example:
Before - 0.00004320
After - 0.00004320
Any help or pointers in the right direction much appreciated.
Thanks.
You can do it using a loop and some string manipulation:
function doTheThing($number) {
$length = strlen($number);
$period = strpos($number, '.') + 1;
for ($i = $period; $i < $length; $i++) {
if ($number[$i] !== '0') {
$number = substr($number, 0, $i).'<b>'.substr($number, $i).'</b>';
break;
}
}
return $number;
}
echo doTheThing('0.00004320');
Or you can do it using a regular expression replacement:
function doTheThing($number) {
return preg_replace('/^(\d+\.0*)(\d+)$/', '$1<b>$2</b>', $number);
}
echo doTheThing('0.00004320');
echo doTheThing(sprintf('%.8f', 0.00004320)); // if your number is not a string

Categories