Quick question about modulo

  • Thread starter AngelofMusic
  • Start date
In summary, There is a trend in the hashing function where x*(x+1)%5 only produces remainders of 0, 1, and 2. This is due to the fact that for a prime number, x^2+x takes at most \frac{p+1}{2} values mod p. This can be proven by pairing off all but one of the numbers in the function and showing that they have the same residue.
  • #1
AngelofMusic
58
0
I'm doing review exercises for my computer science class and we're discussing hashing functions. One of them involves quadratic probing and I came upon an interesting trend. Whenever I have:

x*(x+1)%5 where % stands for the modulo operator, and x > 0 and is an integer, I can never seem to get any results except 0, 1 and 2.

Is there some inherent property of [tex]x^2+x[/tex] such that whenever it is divided by 5, it only produces remainders in 0, 1, and 2? This isn't part of the homework, and the trend has held up for the few data points I tried, but I'm wondering if there's any number x that would give another remainder such as 3 or 4.

I tried proving by induction, but that got me nowhere. I tried reasoning it out that: x(x+1) will always be even, since it will be even number * odd, but that means that doesn't really narrow down the possible remainders.

I don't need a rigorous proof or anything, but can anyone explain why this is? I haven't taken number theory so I don't know much about this.
 
Physics news on Phys.org
  • #2
The only possible results are 0,1 and 2 - you only need to check the residues mod 5.

In general, for a prime [tex]p[/tex], [tex]x^2+x[/tex] takes at most [tex]\frac{p+1}{2}[/tex] values mod [tex]p[/tex]

I can pair off all but one of the numbers so that for each pair, [tex]x,y[/tex] , [tex](x+y)\equiv-1[/tex]
Then I can multiply both sides by [tex](x-y)[/tex]
[tex](x+y)(x-y) \equiv -1 \times (x-y)[/tex]
[tex] x^2-y^2\equiv y-x[/tex]
move the [tex]x[/tex]'s and [tex]y[/tex]'s together:
[tex]x^2+x=y^2+y[/tex]
So [tex]x[/tex] and [tex]y[/tex] have the same residue.
 
  • #3


Hi there,

Thank you for reaching out with your question about modulo and quadratic probing in hashing functions. It's great to see that you are actively reviewing and trying to understand the concepts in your computer science class.

To answer your question, there is indeed a property of x^2+x that leads to the trend you are observing. This property is known as the "binomial theorem" and it states that when you expand the expression (x+1)^2, you will get x^2+2x+1. When you divide this by 5, you will see that the remainder can only be 0, 1, or 2.

To understand why this is the case, let's look at the expansion of (x+1)^2 in terms of modulo 5:

(x+1)^2 = (x^2 + 2x + 1) % 5

Now, let's consider the possible remainders for each term:

x^2 % 5 can only be 0, 1, 2, 3, or 4
2x % 5 can only be 0, 2, or 4
1 % 5 can only be 1

So, when we add these terms together, we can see that the only possible remainders are 0, 1, or 2. This explains why you are only getting these results in your exercise.

I hope this helps to clarify why this trend is occurring. Keep up the great work in your studies!
 

1. What is the definition of modulo?

The modulo operation, denoted by the % symbol, returns the remainder of a division between two numbers. For example, 7 % 3 would return the value of 1.

2. How is modulo used in programming?

Modulo is commonly used in programming to determine whether a number is even or odd, to calculate leap years, and to perform various mathematical operations.

3. Can negative numbers be used in modulo?

Yes, both negative and positive numbers can be used in modulo calculations. The result will always be a positive number.

4. What is the difference between modulo and division?

The main difference is that division returns the quotient of a division, while modulo returns the remainder. For example, 7 / 3 would return 2, while 7 % 3 would return 1.

5. Are there any special rules for using modulo?

One important rule to note is that when using modulo with negative numbers, the result may vary depending on the programming language or calculator being used. It is important to check the documentation or test the operation beforehand to avoid unexpected results.

Similar threads

Replies
11
Views
455
Replies
5
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
899
  • Precalculus Mathematics Homework Help
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
437
  • Calculus and Beyond Homework Help
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
11
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
17
Views
4K
Replies
11
Views
2K
Back
Top