Explanation of 1 mod 3 - math

So i've been looking into modulo recently. I'm trying to improve my math skills, which are not the best if i'm honest. But something i am trying to improve. I understand how this works i think. I am also quite competent with long division. However something is bugging me and i can't seem to find an answer for it online.
I know that 7 % 5 = 2 (5 goes into 7 once, with a remainder of 2).
What i don't understand is this;
1 % 3 = 1
How can this be, 3 goes into 1, 0 times, with a remainder of 3? Surely the answer to 1 % 3 = 3?
Can anyone explain this in its most simplest terms please?
Am i correct in thinking that if the dividend (1) is less than the devisor (3) which we know will equal 0 remainder x, it just uses the dividend as the result?
Thanks for your help.

The remainder in 1%3 refers to what remains of 1 (not 3) after you divide by 3. As you have already said, 3 goes into 1 zero times. So -- when you remove 0 multiples of 3 from 1, all of 1 remains. Thus 1 % 3 = 1.

The result of a modulo operation n % m is just that number r for which q * m + r = n (q may be anything). The only requirement we have is that 0 <= r < m.
So for instance:
7 % 5 --> 1 * 5 + 2 == 7 --> r = 2
1 % 3 --> 0 * 3 + 1 == 1 --> r = 1

Related

trouble converting index to row

I'm having trouble converting an index number into its respective column/row. The table goes like this
The graph scales in each dimension. Each square is surrounded by one blank space. I need to turn the number of the square into the x/y coordinates
I've figured out the column, but the row is still evading me.
This is what i have now:
#define IDtoX(n, w) ((2*(n%w))+1)
#define IDtoY(n, h) ((2*(n/h))+1)
IDtoX works as intended. IDtoY does not.
outputs should be as following.:
grid of width 7 and height 5:
n y
0 3
1 3
2 3
3 1
4 1
5 1
grid of width 9 and height 7:
0 5
1 5
2 5
3 5
4 3
5 3
6 3
7 3
8 1
9 1
10 1
11 1
The main reason why you are failing with your function IDtoY(n, h) is that the result also depends on the value of w. Therefore you must change your signature to something like IDtoY(n, w, h). To see this, try drawing more arrays with the same hs but varying ws and you will see that the ids will also change for each n. You were fooled by your successful function for IDtoX which does indeed not depend on h but only on n and w. Now, if your ids began at zero at the top, they would not depend on w, but as you drew the array, they do.
I found multiple formulae that work, but none of them are pretty. Here are two--if you do not like them, you could find some equivalent formulae.
#define IDtoY(n, w, h) (h - 2 - 2 * n // (w - 1) * 2)
or perhaps
#define IDtoY(n, w, h) (h - 2 - n // (w // 2) * 2)
where the // operator is integer division. You do not state which computing environment you are using--the simple / operator may work for you.

How to quickly tell if an "unknown" number is divisible by 3?

I'm trying to tackle down a problem where the time limit is very low (1 second) and the number of cases is supposedly high.
You need to tell if a number is divisible by 3, but the problem is that you don't get the direct number, you get a number k, and then need to check if the concatenation of numbers from 1 to k (123...k) is divisible by 3.
Example input:
4 // The number of cases
2
6
15
130000000
Output:
YES // Because 12 is divisible by 3
YES // Because 123456 is divisible by 3
YES // Because 123456789101112131415 is divisible by 3
NO
I've found some topics about quickly checking the divisibility, but what most time takes I think is to build the number. There are cases where the initial number is as high as 130000000 (so the final is 1234...130000000) which I thinks overflows any numeric data type.
So, what am I missing here? Is there any way to know if something is divisible by 3 without concatenating the number? Any ideas?
PD: Someone also posted the triangular numbers formula which also is a correct solution and then deleted the answer, it was:
if ((1 + num) * num / 2) % 3 == 0 ? "YES" : "NO"
Every third number is divisible by three.
Every number divisible by three has a digit sum divisible by 3.
Every third number has a digit sum divisible by 3.
In between these, every third number has a digit sum congruent to 1 and then 2 mod 3.
Take a look:
n digit sum mod 3
0 0
1 1
2 2
3 0
4 1
5 2
6 0
...
10 1
11 2
12 0
...
19 1
20 2
21 0
...
Say we have a string of digits constructed as you describe, and the number we just added was divisible mod 3. When we append the next number's digits, we are appending digits whose sum is congruent to 1 mod 3, and when added to those in our number, we will get a combined digit sum congruent to 1 mod 3, so our answer for the next one will be "no". The next one will add a number with digit sum congruent to 2 mod 3, and this causes the total to become congruent to 0 again, so the answer here is "yes". Finally, adding the next number which must be divisible by 3 keeps the digit sum congruent to 0.
The takeaway?
if n is congruent to 0 modulo 3, then the answer is "yes"
if n is congruent to 1 modulo 3, then the answer is "no"
if n is congruent to 2 modulo 3, then the answer is "yes"
In particular, your example for n=15 is wrong; the digit string obtained represents a number that should be divisible by 3, and indeed it is (try it on a big enough calculator to verify).
All that is left is to find an implementation that is fast enough and handles all the required cases. If n is guaranteed to be under ~2 billion, then you are probably safe with something like
return (n % 3) != 1;
If n can be an arbitrarily large number, never fear; you can check whether the digit sum is congruent to 0 modulo 3 by adding up the digits in linear time. If not, you can add 1 from the number by coding addition like you do it by hand on paper and then check the result of that for divisibility by 3, again in linear time. So something like:
if (digit_sum_mod_3(n) == 0) return true;
else if (digit_sum_mod_3(add_one(n)) == 0) return false;
else return true;
Then you would have something like
digit_sum_mod_3(n[1...m])
sum = 0
for k = 1 to m do
sum = sum + n[k]
// keep sum from getting too big
if sum >= 18 then
sum = sum - 18
return sum % 3
add_one(n[1...m])
// work from right to left, assume big-endian
for k = m to 1 do
if n[k] < 9 then // don't need to carry
n[k] = n[k] + 1
break
else then // need to carry
n[k] = 0
if n[1] = 0 then // carried all the way to the front
n[1] = 1
n[m+1] = 0
return n
Any three consecutive numbers sum up to 0 == a + a + 1 + a + 2 mod 3.
The answer reduces to k%3 == 0, or 2k-1 % 3 == 0. The latter is equivalent to k%3 == 2, which leaves out k%3==1 which then simplifies further to k%3 != 1.
It is a known trick in mathematics that a number is divisible by three if the sum of its individual decimal digits is divisible by three.
Example:
2271
2+2+7+1 = 12
12 is divisible by 3, therefore so is 2271
Additionally, the sum of any three consecutive integers must be divisible by three. This is because:
((n)+(n+1)+(n+2))/3 = (3n+3)/3 = n+1 = integer
Therefore:
If k mod 3 == 0, then concatenation of 1 to k is divisible by three.
If k mod 3 == 1, then concatenation of 1 to k is not divisible by three.
If k mod 3 == 2, then it is a bit trickier. In this case, concatenation of 1 to k is divisible by three if the sum of k and the number before k (which evaluates to (k)+(k-1), which is 2k-1) is divisible by three.
Therefore, the final condition is:
(k mod 3 == 0) || ((k mod 3 == 2) && (2k-1 mod 3 == 0))
However, this can be even further simplified.
It turns out that k mod 3 can only equal 2 whenever 2k-1 mod 3 equals 0 and vice versa.
See simple graph below that shows cyclic pattern of this behavior.
Therefore, the formula can be further simplified just to:
(k mod 3 == 0) || (k mod 3 == 2)
Or, even more simply:
(k mod 3 != 1)
I realize answerer already provided this answer so I don't expect this to be the accepted answer, just giving a more thorough mathematical explanation.
A number is divisible by three if the sum of its digits is divisible by three (see here). Therefore, there is no need to "construct" your number, you need simply add the digits of the individual numbers. Thus for your 15 case, you do not need to "construct" 123456789101112131415, you just need to sum all of the digits in [1, 2, 3, 4, ... 14, 15].
This is simpler than it sounds because the problem only needs to check numbers of a very specific format: 12345789101112131415…k. You can use Gauss's method to quickly get the sum of the numbers 1 to k and then check if that sum is divisible by three using the usual methods. The code for that is:
'NO' if (k*(k+1)/2)%3 else 'YES'
If you look at the pattern that occurs as k increases (NO, YES, YES, NO, YES, YES, ...), you don't even need the multiplication or division. In short, all you need is:
'YES' if (k-1)%3 else 'NO'
Here is Python code which reads integers from a file and, if it wouldn't take too long also checks the answer the hard way so you can see that it is right. (Python numbers can be infinitely long, so you don't need to worry about overflow):
#!/usr/bin/python3
# Read integers from stdin, convert each int to a triangular number
# and output YES (or NO) if it is divisible by 3.
def sumgauss(x):
'''Return the sum from 1 to x using Gauss's shortcut'''
return (x*(x+1)/2)
def triangle(n):
'''Given an integer n, return a string with all the integers
from 1 to n concatenated. E.g., 15 -> 123456789101112131415'''
result=""
for t in range(1, k+1):
result+=str(t)
return result
import sys
for k in sys.stdin.readlines():
k=int(k)
print ( 'YES' if (k-1)%3 else 'NO', end='')
# If it wouldn't take too long, double check by trying it the hard way
if k<100000:
kstr=triangle(k)
print("\t// %s modulo 3 is %d" % (kstr, int(kstr)%3))
else:
print('\t// 123456789101112131415...%d%d%d modulo 3 is %d' %
tuple([k-2, k-1, k, sumgauss(k)%3]))
Speaking of Gauss's shortcut for summation, this problem seems a lot like a homework assignment. (Gauss invented it as a student when a teacher was trying to get the class out of his hair for a while by making them add up the numbers from 1 to 100.) If this is indeed a class assignment, please make sure the teacher knows to give the A to me and stackoverflow. Thanks!
Sample output:
$ cat data
2
6
15
130000000
130000001
$ ./k3.py < data
YES // 12 modulo 3 is 0
YES // 123456 modulo 3 is 0
YES // 123456789101112131415 modulo 3 is 0
NO // 123456789101112131415...129999998129999999130000000 modulo 3 is 1
YES // 123456789101112131415...129999999130000000130000001 modulo 3 is 0
The first 32 triangular numbers:
$ seq 32 | ./k3.py
NO // 1 modulo 3 is 1
YES // 12 modulo 3 is 0
YES // 123 modulo 3 is 0
NO // 1234 modulo 3 is 1
YES // 12345 modulo 3 is 0
YES // 123456 modulo 3 is 0
NO // 1234567 modulo 3 is 1
YES // 12345678 modulo 3 is 0
YES // 123456789 modulo 3 is 0
NO // 12345678910 modulo 3 is 1
YES // 1234567891011 modulo 3 is 0
YES // 123456789101112 modulo 3 is 0
NO // 12345678910111213 modulo 3 is 1
YES // 1234567891011121314 modulo 3 is 0
YES // 123456789101112131415 modulo 3 is 0
NO // 12345678910111213141516 modulo 3 is 1
YES // 1234567891011121314151617 modulo 3 is 0
YES // 123456789101112131415161718 modulo 3 is 0
NO // 12345678910111213141516171819 modulo 3 is 1
YES // 1234567891011121314151617181920 modulo 3 is 0
YES // 123456789101112131415161718192021 modulo 3 is 0
NO // 12345678910111213141516171819202122 modulo 3 is 1
YES // 1234567891011121314151617181920212223 modulo 3 is 0
YES // 123456789101112131415161718192021222324 modulo 3 is 0
NO // 12345678910111213141516171819202122232425 modulo 3 is 1
YES // 1234567891011121314151617181920212223242526 modulo 3 is 0
YES // 123456789101112131415161718192021222324252627 modulo 3 is 0
NO // 12345678910111213141516171819202122232425262728 modulo 3 is 1
YES // 1234567891011121314151617181920212223242526272829 modulo 3 is 0
YES // 123456789101112131415161718192021222324252627282930 modulo 3 is 0
NO // 12345678910111213141516171819202122232425262728293031 modulo 3 is 1
YES // 1234567891011121314151617181920212223242526272829303132 modulo 3 is 0
Actually the answer is pretty straight forward, if the sum of the digits divisible by three then the number is also divisible by 3.
string ans=(((1 + num) * num) / 2) % 3 == 0 ? "YES" : "NO";
according to the problem sum of digit can be considered as sum of numbers from 1 to n, sum=(n*(n+1))/2
*Make sure you divide the whole thing by 2
Another approach:
string ans=n % 3 !=1 ? "YES" : "NO";
You can prove that if n or n-2 is divisible by 3, then the sum up to n is divisible by 3 (e.g., in your case sum(1...8), sum(1..9), sum(1..11), etc.).

How do I find a list (multiset, size n) of integers where the root-mean-square of the set is an integer?

I already found this one
Brute force is possible of course, but are there any other ways? Is there a way to find all multisets? Is there a way to find out how many combinations exist under a certain limit?
Perhaps this question is too mathy for SO, if that is the case I'll move it.
I created my own version in javascript by generating all possible combinations of a list of numbers, then checking for integer RMS. These are sets though, not multisets.
Edit: I used N for sum value and K for the number of squares.
Number of multi-sets grows fast, so N should have reasonable value. So this problem is equivalent to changing sum N by K coins with nominals 1,4,9,25... (and the number of variants might be calculated using dynamic programming).
The simplest implementation is recursive - we just generate all possible addends. In my Delphi implementation I collect summands in string (instead of list) for simplicity.
Note that such implementation might generate the same sequences again and again - note {5,7} end-sequence in my example. To improve performance (important for rather large values of N and K), it is worth to store generated sequences in table or map (with {N;K;Min} key). In that case generation from large summands to smaller ones would be better, because small summands give a lot of repeating patterns.
procedure FSP(N, K, Minn: Integer; Reslt: string);
var
i: Integer;
begin
if (K = 0) then begin
if (N = 0) then
Memo1.Lines.Add(Reslt); //yield result
Exit;
end;
i := Minn;
while (i * i <= N) do begin
FSP(N - i * i, K - 1, i, Reslt + Format('%d ', [i]));
i := i + 1;
end;
end;
procedure FindSquarePartitions(N, K: integer);
begin
FSP(N, K, 1, '');
end;
FindSquarePartitions(101, 5);
1 1 1 7 7
1 1 3 3 9
1 1 5 5 7
1 2 4 4 8
1 5 5 5 5
2 2 2 5 8
2 3 4 6 6
2 4 4 4 7
3 3 3 5 7

Why is 3-1*8+2*3 equal to 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I used PEMDAS on 3 - 1 * 8 + 2 * 3.
Steps:
1) 1 * 8 = 8
2) 2 * 3 = 6
3) 8 + 6 = 14
4) 3 - 14 = -11
Multiply all the terms, then add and finally subtract but I get -11 as the result.
But when I googled it, it said 1. Where did I go wrong?
By Following the BODMAS rule -
3 - 1 * 8 + 2 * 3
So According to BODMAS Rule-
B → Brackets first (parentheses)
O → Of (orders i.e. Powers and Square Roots, Cube Roots, etc.)
DM → Division and Multiplication (start from left to right)
AS → Addition and Subtraction (start from left to right)
So our equation ->
3 - (1 * 8) + (2 * 3)
3-8+6
-5+6
1
After multiply all the terms,please follow the order of operations.Right steps:
Steps: 1) 1 * 8 = 8 2) 2 * 3 = 6 3)3-8=-5 4)-5+6=1
The order of operations is, as you said it PEMDAS (or BEDMAS whatever you like), but it is also left-to-right. And on top of that: multiplication and division are treated as the same order and so are addition and subtraction.
So your first two steps were right.
3 - (1 * 8) + (2 * 3)
3 - 8 + 6
Now here is where the left-to-right ordering takes place.
((3 - 8) + 6)
(-5 + 6)
1
To make it easier, you can remember that x - y is really just x + (-y). Then the order of subtraction and addition doesn't matter at all.
You are not following the order of operations correctly. You are correct in doing the multiplication first. However, you also must always go from left to right when performing operations of the same type (addition and subtraction or multiplication and division). By using parenthesis, we can make this clearer.
3 - (1*8) + (2*3) => 3 - 8 + 6 => -5 + 6 => 1
Your mistake is in step 3. The - gets applied to 8 so step 3 is: -8 + 6 = -2

Math question: How know section number on a list

Imagine I have this list, that is divided by 3
1
2
3
4
5
6
7
8
9
Now, I have 9 items, grouped in 3 sections.
My question is how know in which section is 6 (ie: 6 belong to section 2, 2 to section 1, 9 to section 3)
Hmmm...... section = ((item-1) / 3) + 1
section = ceiling (n / 3)
For example,
ceiling (4 / 3) = ceiling ( 1.33 ) = 2
For a list of items divided into sections of size n, the section s of an item i is given by:
s = (i + (n-1)) / n,
where the / is integer division.
So, for your example, item 6 gives (6 + (3-1))/3 = (6+2)/3 = 8/3 = 2.
This applies to many other things as well - I encountered it as "How many nodes do I need to request on a cluster with n CPUs per node?"
I'm not totally sure what you're asking, but give this a try:
floor((itemNumber - 1)/numberOfGroups) + 1

Resources