finding minimum congruent integer

6 views (last 30 days)
K G
K G on 20 Feb 2016
Edited: John D'Errico on 20 Feb 2016
Hello,
Is there any function that gives the min congruent of an integer?
For example, the congruents of "23 mod 3" are the numbers: 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, ... ( WolframAlpha 's result )
The minimum congruent is 5.
Is there a function that takes as inputs 23 and 3, and gives as output this minimum congruent 5 ?

Accepted Answer

John D'Errico
John D'Errico on 20 Feb 2016
Edited: John D'Errico on 20 Feb 2016
No. I'm afraid that you are wrong here. The set of numbers that are congruent to 23, modulo 3 are not as you describe, since you missed one (in fact, many) in that list. You forgot to write 2 in the list. Worse, you forgot to include -1, -4, -7, -10, ...
So the "minimum congruent" number that you refer to does not exist, since the set goes on ad infinitum. Infinity is a long way.
Ok, perhaps you wish to know the minimum non-negative number? Trivial. MOD gives that directly.
mod(23,3)
ans =
2
That is indeed the MINIMUM NON-NEGATIVE congruent number to 23, modulo 3.
So, perhaps you really wanted something that you did not say. Perhaps you actually wanted the second smallest (NOT the minimum) positive number that is congruent to 23, modulo 3? Clear explanations of what a person actually wants are so useful sometimes. I'd love to see one in a question, just once. :)
mod(23,3) + 3
ans =
5
Are you asking for a FUNCTION that does it? Ok, a function handle is easiest to write, that takes only one line.
MC = @(N,K) mod(N,K) + K;
MC(23,3)
ans =
5
Or, if you wish to see the entire set of non-negative congruents (I'll need to list only a few, as infinitely many numbers take a while to display)...
mod(23,3) + 3*(0:10)
ans =
2 5 8 11 14 17 20 23 26 29 32
So, whatever you really want, the answer is probably above. I'm just not positive that you are sure that you know what you want. :)
  1 Comment
K G
K G on 20 Feb 2016
I am sorry for the incomplete description, I am indeed looking for the minimum positive congruent.
I 'm also sorry for the misunderstanding of the term "congruent", I am new in this.
Thank you for your answer.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!