I want to check if x divided by y is a whole number, but if I just do x / y, it gives me an integer. But if I do x.to_f / y, it gives me a float, even if it's a whole number.
How can I divide a number and only get a float if it's not a whole number?
Basically what I'm trying to do is check if a number is divisible by another, so if you have a better way of doing this please let me know :)
Found out
if x % y == 0 it's divisible
Or you might want to use
x.modulo(y).zero?
Related
I have an exercise where I am supposed to multiply an unknown number by 5. The number is max 8 bits and stored in 2's compliment. So if the number is 5 the result should be 25 and if its -12 it should be -60.
My only tools are load a register with a bit pattern and add two bit patterns together and store the result. I'm only allowed to use addition 3 times in my program.
With a known number I think I could solve it by adding the appropriate bit pattern to make it 5 times bigger but will that work with an unknown number?
Ex. I have the bit pattern 0001010 representing 10 knowing I want 50 as the result I would add the bit pattern 0101000 to get the resulting 0110010 which is 50.
Can someone please give me a hint how I can solve this problem thank you!
You can compute 5x=2x+2x+x using three additions. Something like this:
y = x + x // y = 2x
z = y + y // z = 2y = 4x
r = z + x // r = 4x + x = 5x
If x is the unknown value, then at the end of this code, r is x * 5.
I have recently participated in a coding contest where one of the problems was as follows:
Given two integers X and Y, find the minimum number of steps required to convert X to Y. You can perform the following operations any number of times in any order:
1) Divide X by any integer A, 2)Multiply X by any integer B.
Example: If X=15 and Y=10, then first multiply X by 2 which gives 30 and then divide 30 by 3 to get Y(i.e.,10). So the minimum no. of steps in this case is 2.
I have no idea how to solve it.
As stated, the minimum number of steps is no greater than two: you can always choose A=X and B=Y so that X/A*B = X/X*Y = Y.
The only times you can do better than this are the following:
if X % Y = 0, the minimum number of steps is 1 and the correct choice is A=(X/Y).
if Y % X = 0, the minimum number of steps is 1 and the correct choice is B=(Y/X).
if X = Y, the minimum number of steps is 0 as no multiplications or divisions are required at all.
I have a simple model in IBM ILOG CPLEX.
dvar float x in 1..99;
dvar float y in 1..99;
dvar float z in 1..99;
subject to
{
x + y - z == 41.3;
}
I need random solutions for x, y and z. However, I always get 41.3, 1, 1.
Am I using the wrong tool?
Moreover, I need five random solutions. Not only one. How can I accomplish this?
For a feasibility problem (no objective function) CPLEX will terminate when it finds a feasible solution. There is no way to obtain all extreme points.
What you could try:
set an objective function
solve and store solution
modify the objective function to find a different solution (which has to be done randomly, if you want random solutions)
You would have to use some API to code the logic.
This idea is described in more detail here:
http://orinanobworld.blogspot.de/2013/02/finding-multiple-extreme-rays.html
But, this is way to complicated for your problem. I'd simply do the following:
set z randomly
calculate x + y = z + 41.3
select a random r between 0 and 1
x = (x+y) * r
y = (x+y) * (1-r)
i have following quiz:
Let x be an integer larger than the odd number q. Change the value of x using the following rule
if x is even
then x / 2
else x – q
until x becomes smaller than q
If the final value of x is zero, what can you say about the original input value?
I am thinking about one thing: if x is odd or x=2*k+1 and we are subtract also odd number, we get even. Also I want to note, that unless x is power of 2, at some step dividing by 2, we get odd number. Let take q=11; x>11;let's take x=23; because x=23 is odd, we will have x=x-q x=23-11=12; now x is even so we will have x/2=6<11, so here we can't determine which value of x is about,but if x=22, then we will have x=x/2=11 x=11 is odd, so we will have x-q=0--> it means that x is multiple of q, but which one odd or even number? Let's take x=33; x is odd so x=x-11=22 it is even x=x/2=11, it is odd so x-q=0; no does it means that x is multiple of q?
Yes, it is apparently that x is multiple of q.
I'm trying to round a number to the next smallest power of another number. I'm not particular on which direction it rounds, but I prefer downwards if possible.
The number x that I'm rounding will satisfy: x > 0, and usually fits within the range 0 < x <= 1. Only rarely will it be above 1.
More generally, my problem is: Given a number x, how can I round it to the nearest integer power of some base b?
I would like to be able to round towards arbitrary bases, but the ones I'm most concerned with at the moment is base 2 and fractional powers of 2 like 2^(1/2), 2^(1/4), and so forth. Here's my current algorithm for base 2.
double roundBaseTwo(double x)
{
return 1.0 / (1 << (int)((log(x) * invlog2))
}
Any help would be appreciated!
You've got the right idea; for any base x, x ^ floor( log_x(n) ) is what you want. (Where log_x represents 'log to the base x')
In C#:
static double roundBaseX(double num, double x)
{
return Math.Pow(x, Math.Floor(Math.Log(num, x)));
}
If you can't take logarithms to an arbitrary base, just use the formula: log_x(n) = log(n) / log(x)