What does this operator mean? /= - c

I have just come across some code with the line:
n /= 10;
I assumed it was a typo and removed the / to make it n = 10, but the program no longer works.
Never seen this sort of operator before, anybody know?

The /= is a shorthand operator.
a /= b
is equivalent to
c = a/b;
a = c;

The /= is one of the shorthand operators.
A shorthand operator is a concise way to express something that is already available in a programming language.
They are:
+= (E.g.: x += 4; is equivalent to x = x + 4;)
-= (E.g.: x -= 4; is equivalent to x = x - 4;)
*= (E.g.: x *= 4; is equivalent to x = x * 4;)
/= (E.g.: x *= 4; is equivalent to x = x / 4;)
%= (E.g.: x %= 4; is equivalent to x = x % 4;)

n /= 10 is taking the value of n, dividing it by 10 and reassigning that value to n.
It's just shorthand for n = (n / 10) just like n++ is n = n + 1.

It is the same as
n = n/10 ;
You can use this form also with other operators (+, -, %,...).
The case n+=1 (n-=1) can also be written as n++ (increase n by 1) or ++n.

It simply means n = n/10. The same form can be used with other operators too.

Related

Need a formula to calculate (i->0 to n-1 )π(n!/i!)

I need a formula to calculate the expression multiplication of (n!/i!) where i varies from 0 to n-1. So that I can implement it to a Code. trivial way exceed time limit.Any quick suggestions are welcome
x = (n!/i!) = (i+1) * (i+2) * ... * (n)
So, in pseudo-code (doing it in reverse order for increased efficiency):
x = 1;
result = 1;
for (j = n; j > 0; j--) {
x = x * j;
result = result * x; // Or + if you want the sum instead of the product
}
return result;
Note that you may need a bigint representation for x and result as they will overflow quickly.

Finding the minimum value to satisfy a modulus

The problem I have is x = (16807 x k) % 65536
ie 16807k ≡ x (mod 65536)
I need to calculate k knowing x.
My best effort so far is something of a brute force. Is there a mathematical way to calculate k?
If not any optimisations on my current code would be appreciated.
t = x;
while ( t += 15115 ) // 16807k = 65536n + x - this is the n
{
if (t%16807 == 0)
return t/16807;
}
return x;
EDIT: Changed += to 15115
An odd numbers has a multiplicative inverse modulo a power of two.
The inverse of 16807 mod 216 is 22039.
That means that (16807 * 22039) % 65536 == 1, and consequently, that
(16807 * 22039 * x) % 65536 == x
And
k = (22039 * x) % 65536
So you don't have to try anything, you can simply calculate k directly.
You solve this kind of problems using the extended euclidean algorithm for the GCD of 16807 and 65536
The remainder sequence is initiated with
R0=65536
R1=16807
and the computation of the inverse with
V0=0 (V0*16807 == R0 mod 65536)
V1=1 (V1*16807 == R1 mod 65536)
Then using integer long division,
Q1=R0/R1=3,
R2=R0-Q1*R1=15115
V2=V0-Q*V1=-3 (V2*16807 == R2 mod 65536)
Q2=R1/R2=1,
R3=R1-Q2*R2=1692
V3=V1-Q2*V2=4
Q3=8, R4=1579, V4=-35
Q4=1, R5=113, V5=39
Q5=13, R6=110, V6=-542
Q6=1, R7=3, V7=581
Q7=36, R8=2, V8=-21458
Q8=1, R9=1, V9=22039
so that 22039 is found as the modular inverse of 15115 modulo 65536.
If you have to look up k repeatedly for different x, you can build a table of solutions before you start decoding:
uint16_t g = 16807u;
uint16_t *mods = malloc(0x10000 * sizeof(*mods));
int i;
for (i = 0; i < 0x10000; i++) {
uint16_t x = g * i; // x is effectively x mod 2**16
mods[x] = i;
};
The solution to yor equation in the 16-bit-range is then:
uint16_t k = mods[x];
It is assumed that x is a 16-bit unsigned integer. Don't forget to free(mods) after you're done.
If k is a solution, then k+65536 is also a solution.
The straightforward brute-force method to find the first k (k>= 0) would be:
for (k=0; k < 65536; k++) {
if ( (k*16807) % 65536 == x ) {
// Found it!
break;
}
}
if (k=65536) {
// No solution found
}
return k;

how to check if the / operator has no remainder in C?

I want to check if the / operator has no remainder or not:
int x = 0;
if (x = 16 / 4), if there is no remainder:
then x = x - 1;
if (x = 16 / 5), if remainder is not zero:
then x = x + 1;
How to check if there are remainder in C? and
How to implement it?
Frist, you need % remainder operator:
if (x = 16 % 4){
printf("remainder in X");
}
Note: it will not work with float/double, in that case you need to use fmod (double numer, double denom);.
Second, to implement it as you wish:
if (x = 16 / 4), if there is no remainder, x = x - 1;
If (x = 16 / 5), then x = x + 1;
Useing , comma operator, you can do it in single step as follows (read comments):
int main(){
int x = 0, // Quotient.
n = 16, // Numerator
d = 4; // Denominator
// Remainder is not saved
if(x = n / d, n % d) // == x = n / d; if(n % d)
printf("Remainder not zero, x + 1 = %d", (x + 1));
else
printf("Remainder is zero, x - 1 = %d", (x - 1));
return 1;
}
Check working codes #codepade: first, second, third.
Notice in if-condition I am using Comma Operator: ,, to understand , operator read: comma operator with an example.
If you want to find the remainder of an integer division then you can use the modulus(%):
if( 16 % 4 == 0 )
{
x = x - 1 ;
}
else
{
x = x +1 ;
}
use the % operator to find the remainder of a division
if (number % divisor == 0)
{
//code for perfect divisor
}
else
{
//the number doesn't divide perfectly by divisor
}
use modulous operator for this purpose.
if(x%y == 0) then there is no remainder.
In division operation, if the result is floating point, then only integer part will be returned and decimal part will be discarded.
you can use Modulous operator which deals with remainder.
The modulus operator (represented by the % symbol in C) computes the remainder. So:
x = 16 % 4;
x will be 0.
X = 16 % 5;
x will be 1

Trying to get this for loop to work?

I just started to learn C so the answer is probably incredibly obvious but when I run this code the number 0 just keeps repeating in an infinite loop. I'm trying to print x from 0 to 1 in increments of .05.
#include <stdio.h>
int main()
{
double x;
for( x = 0; x <= 1; x+.05 )
{
printf("%d\n", x );
}
}
for( x = 0; x <= 1; x += .05 )
seems like your not writing the changed x value to x..... If you know what I mean :D
x++ is the same as x+=1
x+.05 doesn't modify x's value, thus x will always be 0 and result in a infinite loop...
I think that's what you're looking for:
for( x = 0; x <= 1; x+=0.05 )
{
printf("%f\n", x );
}
You'll want to change to the += sign and change the d to an f.
d is for decimal integers
f is for floating point numbers
You want the addition and assignment compound operator, which is +=, not just +.
for( x = 0; x <= 1; x+=.05 )
Currently the result of your expression is x + 5, and its result is not used, resulting in your loop's condition never being false.
Change the line in your for loop to
for( x = 0; x <= 1; x += .05 )
Note that
x += .05
Is equivalent to typing
x = x + .05
which is what you really want since the goal is to update the value of x.

Is `y = x = x + 1;` undefined behavior?

Is this code:
y = x = x + 1;
undefined behavior in C?
Answer to your question
No.
What will happen
This will happen:
int x = 1; /* ASSUME THIS IS SO */
y = x = x + 1;
/* Results: */
y == 2;
x == 2;
How it compiles
The same as:
x += 1;
y = x;
Why this is not undefined
Because you are not writing x in the same expression you read it. You just set it to itself + 1, then assign y to the value of x.
Your future
If you find the code confusing you can use parentheses for readability:
y = x = (x + 1);
No, your expression is properly defined. You probably were looking for y = x = x++;, which is not.
No. You only modify x once, and due to the right-associativity of = that assignment happens before the assignment to y. Even if it did happen after, there's still only one modification of x. Your statement is as legal as y = ++x.

Resources