I have a mathematical question with PHP. Where n is a positive integer, this function f(n) satisfies the following.
This is a question asked in my programming class and now I am now trying to create a program to find f(n) using PHP, but now I am confused because this equation contains more than one function and I do not know how to put this in PHP. If you have any idea on how to put this equation into some codes, please post your idea. I would like to know how to write php code to solve this kind of mathematical equations.
If you look closely to the equation you will find this is Fibonacci Series.you can solve this using recursive function Like this.
function fib($n) {
if ($n < 0) {
return NULL;
} elseif ($n === 0) {
return 0;
} elseif ($n === 1 || $n === 2) {
return 1;
} else {
return fib($n-1) + fib($n-2);
}
}
As you can see i am calling same function until the base condition satisfied. Hope this help
Related
I am currently teaching myself web development/ programming and to learn php i have built a simple program. The program takes user input and based on a series of math algorithms and calculates 7 random lottery numbers. The code is working fine but i want to improve it. The code is very repetitive and i want to simplify it by creating my own functions. I have created the first function that takes the users input, simply does some maths and then returns some values.
For Example...
<?php
function some_maths($int1 $int2 $int3){
$x = $int1 + $int2;
$y = $int2 * $int3;
$z = $y * $x;
return $x
....}
So this is pretty straight forward, but what i want to do now is take the values of X, Y, Z and create a function that checks to make sure they're not matching, or that they're not less than 1 or greater than 59. I used a while loop in my original code that goes like this:
while($x == $y || $x == $z || $x <1 || $x >59){
if( x> 59 || x < 1){
if (x<1){
do{ $x+=$int}while($x <1);
}elseif ($x > 59){
do{ $x-=$int}while($x >59);
}else $x++;
}
This seems to work fine but i don't want to have to repeat the same code over and over. I am sure there has to be a better way? Could i put the values into an array and maybe do it that way? What would be the best solution for this?
Your question is kind of vague but if I had to write a function to check if three numbers weren't equal and were < 59 and >1 this is how I would do it
function validateNumbers($x , $y , $z)
{
if(equal($x,$y)) return false;
if(equal($x,$z)) return false;
if(equal($y,$z)) return false;
if($x>59||$x<1) return false;
if($y>59||$y<1) return false;
if($z>59||$z<1) return false;
return true;
}
function equal($x , $y)
{
if($x == $y)return true;
else return fasle;
}
So far I only see two (pretty straightforward) things:
Your function prototype in the first example is missing commas between the parameters. Instead of function some_maths($int1 $int2 $int3) it should read function some_maths($int1, $int2, $int3).
In your second example a closing } is missing. But if I am interpreting your stuff correctly, the outer if-clause is redundant. Thus, the snippet can be simplified to:
Second example:
while($x == $y || $x == $z || $x <1 || $x >59){
if (x<1){
do{ $x+=$int}while($x <1);
}
elseif ($x > 59){
do{ $x-=$int}while($x >59);
}
else $x++;
}
There may be more room for improvement (e.g. slim down the condition of the outer while loop) - but for that we would need more context (what happens before your loop, what is $int, ...).
Given two functions in PHP, say
function f($n) {
return $n;
}
function g($n) {
return pow($n, (2/3));
}
How to check if a function f(n) is in Ω(g(n)), Θ(g(n)) or O(g(n)) in PHP?
What I tried so far:
$n = INF;
$A = f($n) / g($n);
if ($A == 0) {
echo "f(n) = O(g(n))";
} elseif (is_infinite($A)) {
echo "f(n) = Ω(g(n))";
} elseif ($A != 0) {
echo "f(n) = Θ(g(n))";
}
Shouldn't that work?
Your basic idea is correct: you have to find the limit of f(n)/g(n) as n grows without bound. Unfortunately there is no easy way to compute the exact limit in PHP, since that requires symbolic computations which is best left to a computer algebra system such as Mathematica or Maxima.
You can approximate the limit by computing f(n)/g(n) for increasing values of n and seeing if you get a sequence that approaches a fixed value. For example:
$n=1;
while ($n < 1e300) {
$A = f($n)/g($n);
echo $A, "\n";
$n *= 1e12;
}
In this particular case the sequence of f(n)/g(n) seems to grow without bound, so the numerical evidence suggests that f(n) is in Ω(g(n)). This is not a proof though; symbolic methods are needed for that.
Both the time and space requirements for both f() and g() are in Ω(1), Θ(1) and O(1).
This question is born out of pure laziness and the desire to do more with less code.
Say I have a variable $x which needs to be greater than 0 and less than 12, what is the fastest (least amount of code written) way to check. Is there a faster way than this.
if($x < 0 || $x > 12) {
die("invalid x value");
}
It would be nice (and I think some languages have it) to do this:
if(0 > $x > 12) {
die("invalid x value");
}
Very curious to see if there is some PHP magic I am missing out on.
You can use filter_var as a native PHP function : http://de2.php.net/manual/en/function.filter-var.php But I don't find it any better, as you will need to pass an array with min and max range, which is not fast, nor short.
Maybe a user-defined function for this will fit? Yes, you will need to write the code once, but only once.
function between($value, $from, $to) {
if ($value < $from || $value > $to) {
return false;
}
return true;
}
The function return false, if the value is less than the min bound, or greater than the max bound. Otherwise it returns true. So if you need to stop you script, if the value is NOT between, you would need to ask for the false response if(!between...
So you only call it this way:
$x = 14;
if(!between($x, 0, 12)) {
die("invalid x value");
}
Output:
invalid x value
if, for example your $x is 5 and you want to check if it's between, and if it is - to continue the script, you ask for the true response.
if(between($x, 0, 12)
I can check if some integer value (which comes from user input, and thus should be filtered) is in specific range like this:
<?php
$size=50;
var_dump(in_array($size,range(1,100)));
?>
which will echo true if the size in range 1 to 100. Of course the other method is using filters:
<?php
$size=50;
$int_options = array("options"=>
array("min_range"=>0, "max_range"=>256));
var_dump(filter_var($size, FILTER_VALIDATE_INT, $int_options));
?>
But, what if I need to know if the elements of an array are in this range?(and probably remove those not) What is the best practice, considering the performance and memory usage. I prefer to use php functions instead of writing mine.
Slightly functional approach (I don't know if PHP support lambdas):
function mapper($n) { return $n >= 1 && $n <= 100 ? 1 : 0; }
...
if (array_product(array_map('mapper', $array)) == 1) { }
Not very performance nor memory efficient, though.
For removing, I'd suggest using array_filter.
function my_filter($n) { return $n >= 1 && $n <= 100; }
...
$newarray = array_filter($array, 'my_filter');
(btw, who the hell designed that language.. array_map and array_filter have different order of parameters?!)
If I were you, I will use a more simple approach:
$size=50;
if($size <= 100 && $size >= 1) {
return true;
} else {
return false;
}
No function call, simple integer comparison. Good performance.
I need help with performing a binary search with a search term ($searchTerm) and comparing it to a dictionary ($dictionary).
Basically, it reads a dictionary file into an array. The user inputs some words, that string becomes $checkMe. I do an explode function and it turns into $explodedCheckMe. I pass each term in $checkMe to binarySearch as $searchTerm (Okay, my code is confusing). I think my logic is sound, but my syntax isn't ...
I've been using this a lot: http://us3.php.net/manual/en/function.strcasecmp.php
here is my code: paste2.org/p/457232
I know this doesn't directly answer your question, but have you considered using pspell and a custom dictionary?
So you are looking up exact strings in the dictionary. Why don't you a simple array for this? The native PHP's hash table is definitely going to be faster than a binary search implemented in PHP.
while (!feof($file)) {
$dictionary[strtolower(fgets($file))] = 1;
}
...
function search($searchTerm, $dictionary) {
if ($dictionary[strtolower($searchTerm)]) {
// do something
}
}
But if you really want to use a binary search, try this:
function binarySearch($searchTerm, $dictionary) {
$minVal = 0;
$maxVal = count($dictionary);
while ($minVal < $maxVal) {
$guess = intval($minVal + ($maxVal - $minVal) / 2);
$result = strcasecmp($dictionary[$guess], $searchTerm);
if ($result == 0) {
echo "FOUND";
return;
}
elseif ($result < 0) {
$minVal = $guess + 1;
}
else {
$maxVal = $guess;
}
}
}
The main problem was that you can't set $maxval to $guess - 1. See the wikipedia article on binary search, it's really good.