Math Basics - Getting Started / Overview

https://algo.monster/problems/math-basics

I’m confused. If a = 30 and b= 30, then 30 % 60 + 30 % 60 == 60?
It wouldn’t be 60 % 60

nvm I needed to distribute the c

can someone please break this part down for me?

We can %60 on every number in the array and get [30, 20, 30, 50, 20] and now we can see 30 + 30 = 60. So the two integers whose sum is divisible by 60 are 30 and 150.

Where did we get 30 and 30 from?

1 Like

30 % 60 = 30
150 % 60 = 30

Because last_element = first_element + difference * (number_of_element - 1) , the sum can be expressed as (2 * first_element + difference * (number_of_elements - 1)) * number_of_elements)…

Is not the last part missing the / 2 part

the sum can be expressed as (2 * first_element + difference * (number_of_elements - 1)) * number_of_elements) / 2

because:
The sum of an arithmetic sequence is:
(first_element + last_element) * number_of_element / 2
last_element = first_element + difference * (number_of_element - 1)

so
(first_element + (first_element + difference * (number_of_element - 1))) * number_of_element / 2 or
(2 * first_element + difference * (number_of_elements - 1)) * number_of_elements) / 2

Here’s the animated proof form wikiepedia if you are interested.

“from”

From:

n = 10
for (i = 0; i < 10; i++) {
for (j = 0; j <= i; j++) {
doSomething();
}
}

To:

n = 10
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
doSomething();
}
}

Below is an example in Java

import java.util.Arrays;
import java.util.List;

public class Mod {

public static void main(String[] args) {
	List<Integer> list = Arrays.asList(30, 20, 150, 110, 200);

	Integer offset = 60;

	list.forEach(m -> {

		list.forEach(o -> {
			if (m!=o && (m % offset + o % offset) % offset == 0)
				System.out.println(m);
		});

	});

}

}

test

  1. (first_element + last_element) * number_of_element / 2
  2. last_element = first_element + difference * (number_of_element - 1)
  3. (first_element + first_element + difference * (number_of_element - 1)) * number_of_element / 2 (substituting last_element in eq 1)
  4. (2 * first_element + difference * (number_of_element - 1)) * number_of_element / 2 (simplifying eq 3)

the divided by 2 part is missing in the end for the above mentioned equation

I would format this statement over a few lines. It’s a bit hard to grok when the line break is in the middle of a code statement:
Because last_element = first_element + difference * (number_of_element - 1) , the sum can be expressed as (2 * first_element + difference * (number_of_elements - 1)) * number_of_elements). In big O complexity analysis, we drop the constant terms (first_element, diffence and -1), so this really becomes O(n^2).

I still don’t get modular arithmetic. I don’t understand the properties of it in a equation.

dopecoder is right it should be (2 * first_element + difference * (number_of_element - 1)) * number_of_elements / 2
not
(2 * first_element + difference * (number_of_element - 1)) * number_of_elements

This helped me understand the distributive property of modulo.
https://www.splashlearn.com/math-vocabulary/algebra/distributive-property

The key takeaway from the example in this section of the course for me was that I need to run modulo one additional time, unlike with multiplication. Eg, Typically
(a + b) * m == am + bm

By contrast
(a + b ) % m == (a % m + b % m) % m
I still don’t completely understand WHY I need that third modulo, but c’est la vie.

cuz a % m + b % m could still be greater than m

function mod(x, y) {
while (x >= y) {
x -= y
}
return x
}

Is only works when x is > 0.

Shouldn’t (n+1) + (n+1) + …(n+1) = k(n+1)?

The video shows (n+1) + (n+1) + …(n+1) = n(n+1) which doesn’t make sense?

In the video he discusses how n = number of elements, so if you see that every “grouping” of elements equates to (n + 1), e.g. 2 + (n-1) == (n + 1), 3 + (n - 2) == (n + 1) and so on, then we know that for every element the “grouping” equates (n + 1).

Therefore the answer can be simplified to n * (n + 1).

I’m also lost on this one. I can see that 30 and 150 % 60 == 30, but I don’t see the jump. Was a % 60 done to every single element, and then we looked for the difference or something in the other elements? Maybe if this was written out into the equations? I’m also lost on what a, b, c represents in those equations. Maybe if someone could do a more fleshed out example?