How to generate this series? [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 5 years ago.
Improve this question
What type of series it is and how to generate this by php program?
0 1 3 2 6 7 5 4 12 13 15 14 ...
Observation: The successive difference of the entity is 1
Example:
Difference of 0 and 1 is 1
Difference of 3 and 2 is 1
Difference of 6 and 7 is 1
Difference of 5 and 4 is 1
Difference of 12 and 13 is 1
Difference of 15 and 14 is 1
Please help ...

Its a Decimal Equivalent of Gray code up to n. I have written a code to generate the Gray code for any Number, Use this to generate a series. I have used Javascript, but you can choose any language you want.
Number.toGrayCode = function(n) {
if (n < 0) {
throw new RangeError("cannot convert negative numbers to gray code");
}
return n ^ (n >>> 1);
};
for( var i=0;i<=10;i++)
console.log(Number.toGrayCode(i));

Related

How to get out number - 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 5 years ago.
Improve this question
I want to find out how I can extract $number out of the following code:
$calc = ($number % 10000) / 100;
So what's the opposite of %?
%, or the modulo operator, means "remainder after division by".
For example: 18 % 4 = 2, because if you divide 18 by 4 you get 4 with a remainder of 2. In the other direction, we can see that indeed 4 * 4 + 2 = 18.
Now consider the following:
18 % 4 = 2
22 % 4 = 2
26 % 4 = 2
...
2445678 % 4 = 2
As you can see, there are multiple values that are what we call congruent modulo 4. Therefore an inverse function of modulo cannot exist, because there is an infinite amount of possibilities.
Several cryptographic functions are based on the fact that the above holds true.
Generally you can't. % gives you the modulo of a division, there are mathematically an infinity of numbers that have the same modulo for a given ratio.
It is simply not bijective..

Checking for increments 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 6 years ago.
Improve this question
Using PHP, if I have a starting value, how can I check to see if another value is within a certain number of increments away?
In other words if I start with the value 100 and have an increment of 5, then 90, 95, 105, 110 would all be acceptable, but 104 would not.
if($number %5 == 0) {
//CODE
}
This gets the remainder of $number/5. If it equals 0 then obviously it is divisible by 5 and thus is in your sequence.
You want to take the mod of the inputted number and compare it to the mod of the other number and see if they are the same. For example if you start with 99,
99 % 5 == 4
so now you can see all the increments by checking the mod of them in comparison.
104 % 5 == 4
109 % 5 == 4
So store the mod of the input number vs the mod number and then check to see if it is the same as the mod of the new number. I would write you a php example, but I don't use php.

Mysql php get record where number is between two ranges [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 6 years ago.
Improve this question
I have a table named 'prices' and every range has its price.
course_id start end price
-----------------------------
1 1 5 20
1 5 10 18
1 10 100 15
-------------------------
I have for example $course_id = 1 and $weeks_num = 8.
How can I get the price of that course?
In the given example I should have 18 because 8 exists between 5 and 10.
You can use BETWEEN clause. http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between
...
WHERE 8 BETWEEN start AND end
...

PHP/Maths Percentage of Percentage [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 6 years ago.
Improve this question
I am working out what the changes of a horse winning a race is from the previous records of each horses.
Horse 1 has won 5% of his races
Horse 2 has won 7% of his races
Horse 3 has won 12% of his races
Now if 3 horses are running in this final race, how do I split 100% between these 3 horses?
By doing this I guess :
5 + 7 + 12 = 24
Horse 1 :
5 * 100 / 24 = 20.83
Horse 2 :
7 * 100 / 24 = 29.17
Horse 3 :
12 * 100 / 24 = 50
(20.83 + 29.17 + 50 = 100)

Calculate stars from grades(5-1 and 1-5) [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 8 years ago.
Improve this question
I have grades from 1 to 5 where 1 is the best and 5 is the worst(But not just integral numbers like 1, also 1.14 and so on).
I want to calculate how many stars each grade would be(5 stars would be the best, 1 star the worst - 3 would stay 3 of course - but the rest?)
I googled to get the result but I'm not finding anything helpful. Maybe I'm missing the forest for the trees, I don't know. Basically it would be the reverse number I guess?
I want to do that in PHP.
Pretty straightforward I would think:
$new_grade = 6 - $old_grade;
Or for a more generic solution:
$new_grade = $grade_max + $grade_min - $old_grade;
Star = 6 - Grade
So:
5 → 1
4 → 2
3 → 3
2 → 4
1 → 5

Categories