How to get out number - 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 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..

Related

How to generate this series? [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 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));

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.

Number formating - always 2 to 4 decimals after decimal separator and with thousand separator [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
I have number:
2000
100
0.1
0.15
0.1555
But I need show these number in this format:
2,000.00
100.00
0.10
0.15
0.1555
I will never have more than 4 decimals after decimal separator.
If after decimal separator are only null, I need to display 2 nulls (3. example above).
How I can format these numbers to get required format?
Thank you!
EDIT: I can't use just number_format function, because with this function I can only set how many decimals will be after decimal separator, but as I wrote above - sometimes there will be 2, sometimes 4. So, if my number is 2000.0001 and I set this number_format(2000.0001, 4) output will be 2000.0001, but other number like 0.1 will output 0.1000, but I need it with 0.10 format.
First use number_format to add the commas and get 4 digits after the decimal. Then use a regular expression to remove up to 2 trailing zeroes.
$formatted = preg_replace('/0{1,2}$/', '', number_format($input, 4));
DEMO

questions related to source code of PHP uniqid() [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 7 years ago.
Improve this question
In PHP source code function uniqid() have following C code:
(I removed some types to shorten it)
//...
struct timeval tv;
gettimeofday(&tv, NULL);
int sec = (int) tv.tv_sec;
int usec = (int) (tv.tv_usec % 0x100000);
// The max value usec can have is 0xF423F,
// so we use only five hex digits for usecs.
printf("%08x%05x", sec, usec);
//...
If we put criticism aside, they try to produce 64 bit timestamp.
0xF423F is probably CLOCKS_PER_SEC - 1 (CLOCKS_PER_SEC is decimal 1000000),
but where this 0x100000 come from and what could be the reason to use modulus instead of bitwise and?
She or he could write the Unique ID as printf("%08x%08x", sec, usec)
sample output:
55189926000eb16f
5518997900051219
5518997a0005171b
The zeros in position 8 to 10 are consistent, they don't add entropy, so he wants to get rid of those zeros. The new UID will be 3 bytes shorter with the same entropy. He could simply use printf("%08x%05x", sec, usec);
sample output:
55189926eb16f
5518997951219
5518997a5171b
But that's on the assumption that usec is guaranteed to be less than 0x100000 otherwise UID will be up to 16 bytes long. You need % 0x100000 for insurance. It's also the same as & 0xFFFFF. Technically the insurance should be % 1000000 (decimal), but it doesn't really matter, it's still the same entropy.
Or we could just use the 16 byte version because saving 3 lousy bytes don't matter these days.

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