• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modulus Operator

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I have read through the search result of the Modulus operator and could not find the answer to my question, so, I post my question here.
In mathematic term: a = dq+r, where "a" is an integer and "d" is a dividend and q is a quotient, "r" is the remainder. In java, we can use "%" to get a reminder of a division. e.g
m=101, n=11, 101%11=2, if we just know "m=100" and the reminder (2), how can we get "n" in java?
In reality, everyone can calculte in their head and get the solution, how can I write the code in java to get the solution? e.g 101%n=2, how to solve n?
Waiting for help!
Thanks!
Mindy
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use your example, 101%n =2 -> 101-2 = 99 will be divisble by n.
In other words n will be factor of 99 (n>1).
so 99%n == 0 for all n >1 should be the answer set you are looking for.

HTH.

Originally posted by Mindy Wu:
Hi there, I have read through the search result of the Modulus operator and could not find the answer to my question, so, I post my question here.
In mathematic term: a = dq+r, where "a" is an integer and "d" is a dividend and q is a quotient, "r" is the remainder. In java, we can use "%" to get a reminder of a division. e.g
m=101, n=11, 101%11=2, if we just know "m=100" and the reminder (2), how can we get "n" in java?
In reality, everyone can calculte in their head and get the solution, how can I write the code in java to get the solution? e.g 101%n=2, how to solve n?
Waiting for help!
Thanks!
Mindy

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your example :
101%n=2
so 101=q*n+2 we have q*n=99
so n is the factor of 99
implementation :
for(int i=1;i<=99;i++)
if((99/i)==0){
n=i;
System.out.println("n is : "+n);
}
}
--
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually, my problem a little bit more complicate than this, how to solve if x%10=8, where 8 is a reminder, 10 is the dividen, x must be less than or equal to 16.
Thanks!
[ January 19, 2003: Message edited by: Mindy Wu ]
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am totally confused.
The first time you ask 101%n = 2, how to get n?
Now you ask n%10 = 8, how to get n? (n<=16)
The previous posts have show how to solve question #1.
For #2, n= 10 * m + 8 (m is int.)
and,
0<m<=2 (based on requirement and definition)
So, n=8 is the only solution.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
n = 18 is also a solution. In fact, there are infinite solutions such that n = 10*m + 8, where m is an integer.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the requirement says n <=16, so n=18 is not good.
 
reply
    Bookmark Topic Watch Topic
  • New Topic