Please help make Modulo Simple To Understand - javascript

(Sorry in advance if this has been asked before, I searched and couldn't find a similar question)
So I believe modulo (%) gives me the remainder of a long division equation. so 2%4 =0r.
So in simple terms a modulo equation that equals zero should be an even number. and a modulo equation that equals 1, should be an odd number? Is that correct?
Here's where I begin to confuse myself.
What about equations that equal an even or odd remainder, would that still output an equal or odd number.
For instance. 5%149 equals 4r.. the remainder is an even number, so is the output all even numbers.. or does the very fact that there is any remainder at all mean that the output will be odd numbers??
TLDR, is modulo as simple as 0r outputs even numbers. And anything with 1 or more remainder outputs odd numbers.

Modulo (or modulus) is used to see the remainder of a division.
You can flip it on it's head and use multiplication to help you out as well if necessary. I've provided some examples.
Try doing something like this:
From the equation you posted in your example: 149 % 5 would give you the remainder of 4. Reason for this: The last multiple of 5 you can get before 149 is 145, and your modulus equation is telling you that you have 4 left over.
Now, if you were to do something like 150 % 5, your remainder would be 0 because 150 is a safe multiple of 5.
Some documentation should hopefully help you understand this a bit better as well: https://docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus
Some examples to help you understand the remainder:
10 % 5 = 0 Since 5 x 2 = 10
9 % 3 = 0 Since 9 x 3 = 9
6 % 2 = 0 Since 2 x 3 = 6
7 % 2 = 1 Since you can only multiply 2 three times to get 6, you are left with a remainder of 1.

a modulo equation that equals zero should be an even number. and a
modulo equation that equals 1, should be an odd number?
You've probably seen modulo as a way of testing for evenness but this is not right. It should read
a modulo by 2 operation that equals zero is an even number
ie. x % 2 == 0 implies x is even. Because x is divisible by 2. x % 3 == 0 means x is divisible by 3.
Here's one way I learned to look at it. Consider an analogue clock with n hours on in (perhaps n=12 or n=24 or some other funny clock). The first number in the modulo operation tells you how many hours forward to traverse, going round and round the circle. The second number (n) tells you how many hours is built into the clock.
Here are some examples:
You go forward 5 on a 12 hour clock and land on 5 o'clock.
5 % 12 == 5
You go forward 13, completing one full loop plus one more hour, landing on 1 o'clock.
13 % 12 == 1
You go forward 24, completing 2 full loops but landing at the starting point, 0. (ok most clocks have 12 at the top but its the same as 0.)
24 % 12 == 0
Consider a clock or spinning wheel with 4 categories.
Start at the base and take 7 steps forward. That give you one full traversal (4 steps) then 3 more steps lands you on the 3rd item.
7 % 4 == 3
You've just stepped forward 2. Because the wheel has 4 slots, the counting hasn't reset yet.
2 % 4 == 2
So to recap, the first number is the number of steps to take, the second number is the size of the clock.

Related

Why is JavaScript modulus (%) operator not working as expected?

I am writing a small program which takes as input an integer N and returns true if it is a power of 3. Otherwise, false.
While there are many ways to write such a program, I am particularly interested in ** only ** making following solution work.
First, I find max integer value supported by JavaScript i.e. Number.MAX_VALUE
Then, using this I get max interger value divisible by 3 using formula: Math.floor(Math.log(Number.MAX_VALUE) / Math.log(3)). This comes out to be 646.
So far so good.
Now, idea is that if 3^646 mod N == 0, then N is a power of 3 because 3 is a prime number.
This works perfectly fine on Wolfram Alpha (e.g. http://m.wolframalpha.com/input/?i=%283%5E646%29+mod+27).
Problem:
But I don't get zero when I do the same using JavaScript i.e. Math.pow(3, 646) % 27 returns 5, instead of 0.
Isn't % the modulus operator I am suppose to use?
Am I missing something here?
Thanks :)

having trouble to understand JavaScript 100%3 and answer comes out 1 in console

there,for last few days,i have started learning JavaScript while watching tutorial videos and **`
the mentor mention something that 100 % 3 answer would be 1 in the
console
`** but what i could not get him what he did not describe properly i guess or may be i was to numb to understand.
Can anyone describe properly please whats the issue with 100 % 3 and the answer 1 comes out in the console,so that i could learn JavaScript effective way.
Thank you..
In JavaScript, % is a modulus operator. It returns a reminder after a complete integral division of the numerator by denominator.
So 100 % 3 = 1. 8 % 5 = 3. 14 % 6 = 2 and so on.
If you divide 100 by 3, the answer is 33 with a remainder of 1. The % character gives you the remainder if you would divide the number on the left by the number on the right.
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
etc..

Can anyone explain this process with converting decimal numbers to Binary

I have looked around the internet for a way to convert decimal numbers into binary numbers. and i found this piece of code in some forum.
var number = prompt("Type a number!") //Asks user to input a number
var converted = []; // creates an array with nothing in it
while(number>=1) { //While the number the user typed is over or equal to 1 its shoud loop
converted.unshift(number%2); // takes the "number" and see if you can divid it by 2 and if theres any rest it puts a "1" otherwise "0"
number = Math.floor(number/2); // Divides the number by 2, then starts over again
}
console.log(converted)
I'm not understanding everything completely, so i made some comments of what i think the pieces of code do. But anyone that can explain in more detail? or is the way i think the code does correct?
This code is based on a technique for converting decimal numbers to binary.
If I take a decimal number. I divide it by two and get the remainder which will either be 0 or 1. Once you divide 57 all the way down to 0. You get the binary number for example:
57 / 2 = 28 r 1; 28 / 2 = 14 r 0; 14 / 2 = 7 r 0; 7 / 2 = 3 r 1; 3 / 2 = 1 r 1; 1 / 2 = 0 r 1;
The remainders are the binary number. Sorry if it's a bit hard to read. I definitely recommend writing it out on paper. Read from the last remainder to the first, the remainders look like this: 111001
Reverse it to make it correct. array.unshift() can do this or you could use array.push() then array.reverse() after the while loop. Unshift() is probably a better approach.
57 in decimal is equal to 111001, which you can check.
BTW, this algorithm works for other bases, as long you are converting from decimal. Or at least as far as I know.
I hope this helped.
It seems like you've got the gist of it down.
Let's start with a random number:
6 === 110b
Now let's see what the above method does:
The number is geq than 1, hence, let's add the last bit of the number to the output
6%2 === 0 //output [0]
the number we're working with after dividing the number by two, which is essentially just bit-shifting the whole thing to the right is now 11b (from the original 110b). 11b === 3, as you'd expect.
You can alternatively think of number % 2 as a bit-wise AND operation (number & 1):
110
& 1
-----
0
The rest of the loop simply carries the same operation out as long as needed: find the last bit of the current state, add it to the output, shift the current state.

understanding the modulus operator in javascript

Twenty modulus six is equal to two, which is the remainder but how to know the the modulus is using the 3 to perform the operation?
20 % 6 = 2
You can use
Math.floor(20 / 6);
for this. It rounds down so you have the largest number possible without decimals.
If you want the quotient, you just have to truncate the division
Math.trunc(20/6) //Return 3
Reference
Modulus is not using the 3, it simply knows that:
20 / 6 = 3, with a remainder of 2
=> 20 % 6 = 2
If you want the 3, you just need the expression:
Math.floor(20 / 6)
(or possibly Math.trunc depending on how you want negative numbers handled - floor rounds towards negative infinity, trunc rounds toward zero).

JavaScript - Find out how many times a number goes into another number evenly

Is there a simple way to find how many times a number goes into another number evenly in JavaScript?
Say 11 divided by 4 --- I know 4 goes into 11 2 times evenly
I have this code but I thought there was a simpler way that I maybe forgetting?
<script>
a = 11;
b = 4;
i = a % b;
i2 = a - i;
solution = i2 / b;
document.write(solution); // 2
</script>
What about...
Math.floor(11 / 4);
If you're wanting to handle negative numbers (thanks Ted Hopp), you could use ~~, |0 or any other bitwise trick that will treat its operand as a 32 bit signed integer. Keep in mind, besides this being confusing, it won't handle a number over 32bits.
~~(11 / 4);
You can use this trick:
(a / b) >> 0
Shifting by 0 truncates the fractional part. This will always round toward 0, which Math.floor will not do with negative numbers.

Categories