Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I found this C source here. But needed it in PHP. Don't know how to code. Simply asking for help.
#include <stdio.h>
#define LENGTH 3
void print_binary(int n)
{
int bit = 1<<LENGTH - 1;
while ( bit ) {
printf("%d", n & bit ? 1 : 0);
bit >>= 1;
}
printf("\n");
}
int main(){
int n = 1<<LENGTH, i;
for(i=0;i<n;i++)
print_binary(i);
}
Here is a simple conversion.
<?php
define('LENGTH', 3);
function print_binary($n)
{
$bit = 1<<LENGTH - 1;
while($bit)
{
echo $n & $bit ? 1 : 0;
$bit >>= 1;
}
echo "\n";
}
$n = 1<<LENGTH;
for($i = 0; $i < $n; $i++)
print_binary($i);
?>
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
is there any way to sum/substrate character in php?
For example if
$var1 = 'a';
$var2 = 'b';
$var3 = 'a';
$calculation = $var1 - $var2 + $var3;
echo $calculation;
I want the output as 2a-b
Just like we did in high school algebra?
I wrote a simple function to make something like what you want.
It's just an example, you will have to improve it a lot if you really want to use it, but is a good start.
Limitations:
Only works with letters (Won't work propely if you add numbers, you will have to add that functionaliy).
ALL the letters must have their plus or minus.
You must use spaces before a plus or minus.
This is definitely not the best way to do it, as I said you have to improve it. I wrote it fast but I tested it a bit.
<?
function calc($str){
$data = preg_split("/ /", $str);
$used = Array();
$buffer = "";
foreach ($data as $pos=>$letter){
foreach ($data as $pos2=>$letter2){
if ($letter[1] == $letter2[1] && !in_array($pos, $used) && !in_array($pos2, $used) && $pos != $pos2){
$first = $letter[0] == '+' ? 1 : -1;
$second = $letter2[0] == '+' ? 1 : -1;
$buffer .= ($first+$second).$letter[1];
$used[count($used)] = $pos;
$used[count($used)] = $pos2;
}
}
}
foreach ($data as $pos=>$letter){
if (!in_array($pos, $used)){
$buffer .= $letter;
}
}
return $buffer;
}
echo calc("+a -b +a");
?>
Output:
2a-b
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What's the easiest way to find the first zero bit with PHP?
For example, say I have an integer 47 which is 111101, how can I find out that the 5th bit is the first unset bit? This needs to work to cater for different integers.
$value = 47;
$i = $j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
$i++;
}
echo "bit $i is 0";
If you want to eliminate the use of $i as a counter, you can do a little bit of extra math:
$value = 47;
$j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
}
echo "bit ", (log($j) / log(2) + 1), " is 0", PHP_EOL;
The +1 is necessary because you're starting your binary as bit 1 rather than as bit 0
Use decbin to return a string of 0 and 1.
Then, use strpos to find the first 0 caracters.
$str = decbin(47);
$result = strpos($str, '0');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert a small condition in php to ternary operator.
if($i==2) {
$third="third";
$i=0;
} else {
$third="";
}
How to Translate this to ternary operator?
Thanks.
You can use like this format:
$i==2 ? ($third = "third" AND $i = 0) : ($third="");
Because i'm bored:
list($i, $third) = array(($not2 = ($i != 2)) * $i, ($not2) ? '' : 'third');
or
list($i, $third) = ($i == 2) ? array(0, 'third') : array($i, '');
or maybe
$third = ($i == 2 ? "third" : '') and $i = 0;
or
$i *= !($third = ($i == 2) ? 'third' : '');
But don't do this in real life. Seriously. You have a perfectly readable if; there's no good reason to turn it into a mess like this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a scenario: The $numbers array in the code below contains one thousand numbers(0 - 1000). I need to count how many of these numbers fit into each of the following categories:
how many of them are between 1 and 1000 inclusive,
how many of them are less than 1, and
how many of them are greater than 1000.
I have created a foreach loop to look at each number one after the other, but right now it's treating every number like it belongs in all three categories.
How do I get the counts correct? ie., how many numbers fit into the "Less than 1", "Between 1 and 1000", and "Greater than 1000" categories, respectively.
The current code:
$numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;
}
Very simply just include the conditions. You can just use if
foreach ($numbers as $number) {
if ($number < 1) $count_less_than_one += 1;
else if ($number >= 1 && $number <= 1000) $count_between_one_and_thousand += 1;
else $count_greater_than_thousand += 1;
}