can anyone help me!!!

i need to write a java programthat will figure out if two numbers are relative prime and will give anappropriate message if they are relative prime else it should give the biggest common divisor

example:

input: 6, 35

output: The numbers 6 and 35 are called relative prime

imput: 6, 42

Output:The numbers 6 and 42 are not relative prime and the biggest common factor is 6

please please help me!!

[437 byte] By [ayushgrga] at [2007-11-27 6:51:31]
# 1

> i need to write a java programthat will figure out

> if two numbers are relative prime and will give

> anappropriate message if they are relative prime else

> it should give the biggest common divisor

It might help to define [url=http://en.wikipedia.org/wiki/Coprime]relative primes[/url].

So, which part of this do you need help with? What does your code look like so far?

kevjavaa at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 2

Sure, I'll help.

1) Use a meaningful subject line. We know you're here for help. Just saying "help me" conveys no information except that you don't think before posting. The subject is supposed to provide some indication about what kind of problem you're having, so that people will know whether it's one that's worth their time to read.

2) Try to do it yourself and when you get stuck, describe in detail what you're having trouble with. Just saying "I don't know how to do it" is almost the same as "do my homework for me."

You're welcome.

jverda at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.util.*;

public class RelativePrime

{

public static void main(String []args)

{

int x,y;

Scanner keyboard = new Scanner(System.in);

System.out.println("this progam will check for a relative prime.");

System.out.print("Enter two numbers seperated by a comma to check:");

x= keyboard.nextInt();

y = keyboard.nextInt();

if((x%y==0)&&(x/y==0))

{

System.out.println("the number is prime");

}

else

System.out.println("not prime");

}

}

thnx for replying n i still need to program sumthing that checks a number for relative prime but how!!

ayushgrga at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 4
1) When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.2) What specific problem are you having?
jverda at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 5
ok, ill start on it right away.please provide your full name, and the name of the course, name of the professor so i may put it in header comments.i think i should be able to do it in under an hour, please respond fast
mkoryaka at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 6
> if((x%y==0)&&(x/y==0))How is it that you think this tests whether a number is prime?Java's the least of your worries at this point. You need to understand the steps OUTSIDE of any programming language.
jverda at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 7

for example if i input two numbers 6,35

output should be like: The numbers 6 and 35 are relative prime

and if the imput is 6, 42

output: the numbers 6, 42 are not relative prime and the biggest common factor is 6

i have to program sumthing like that but i am totally confused!!

ayushgrga at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 8
Do you understand how to manually find whether or not the number is a relative prime? That would be a good start.
kevjavaa at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 9

i dont know how to test for a relative prime>> i am sorry and i know how to test a number whether its a prime or not but i dont know how to test a numbers for relative prime

Here is code to check for prime that i did

input = keyboard.nextInt();

if (input < 2 || (input % 2 == 0))

isPrime = false;

if (input == 2)

isPrime = true;

if (isPrime)

for ( i=3; i<=(input/2); i+=2)

if (input % i == 0)

{

isPrime = false;

break;

}

if (isPrime == true)

System.out.println(input + " is a prime number.");

else

System.out.println(input + " is not a prime number.");

ayushgrga at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 10

> for example if i input two numbers 6,35

> output should be like: The numbers 6 and 35 are

> relative prime

>

> and if the imput is 6, 42

> output: the numbers 6, 42 are not relative prime and

> the biggest common factor is 6

Do you think this explains anything?

Seriously, you need to understand what you're trying to do well enough to be able to explain it clearly enough so that somebody who has no idea what "relative prime" means can understand it. If you can't do that, you can't write the code.

jverda at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 11

> Here is code to check for prime that i did

You only need to go up to the square root of x (or maybe sqrt + 1), not up to x / 2.

Also, did you not notice when I said earlier: When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 12

> i need to write a java programthat will figure out

> if two numbers are relative prime and will give

> anappropriate message if they are relative prime else

> it should give the biggest common divisor

>

> example:

> input: 6, 35

> output: The numbers 6 and 35 are called relative

> prime

>

> imput: 6, 42

> Output:The numbers 6 and 42 are not relative prime

> and the biggest common factor is 6

>

> please please help me!!

public class CoPrimeDetector {

private static final String USAGE = "Usage: java CoPrimeDetector <number 1> <number 2>";

private int isPrime(String _numberA, String _numberB) throws NumberFormatException {

return(isPrime(Integer.parseInt(_numberA, Integer.parseInt(_numberB));

}

public int isPrime(int _numberA, int _numberB) {

int prime = 1;

// Please click below LINK ...

[url http://en.wikipedia.org/wiki/Euclidean_algorithm]/* if (_numberA and _numberB are prime) prime = ?; */[/url]

return( prime );

}

public static void main(String[] argv) {

int prime = 1;

if (argv.length != 2) {

System.out.println(USAGE);

System.exit(-1);

}

CoPrimeDetector cpd = new CoPrimeDetector();

cpd.prime = isPrime(argv[0], argv[1]);

if (prime == 1) System.out.println("The numbers "+argv[0]+" and "+argv[1]+" are called relative prime");

else System.out.println("The numbers "+argv[0]+" and "+argv[1]+" are not relative prime and the biggest common factor is "+prime);

}

}

abillconsla at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 13
thnx for ur reply but where do i start putting the sqrt. and how does it help?i really dont get you?By the way i am ayush gurung from nepalthnxMessage was edited by: ayushgrg
ayushgrga at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 14
i am ayush gurung from nepal and my teacher is a nepali as well but i am given to do this exercise and i am really confused help would be greatly appreciated!!
ayushgrga at 2007-7-12 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 15

> thnx for ur reply but where do i start putting the

> sqrt.

That's your stopping condition for the loop. If I want to test if 131 is prime, I only have to go up to 11 or 12, not up to 61.

> and how does it help?

It won't make any difference in the correctness of your program. Depending on which numbers you're testing it might make it go a little faster. The more important thing though is that you understand that and understand why it is so. The better you understand the problem the better your solution will be.

jverda at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 16
thankx abillconsl for your reply,its really so sweet of you but i still get some compile error and i am still trying to figure out the actual problem.. thankx a lotz for your help i really appreciate you....but am still gonna check
ayushgrga at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 17
thanks to all of you for you kind replies but the code is still not workingand i couldnt figure out what exactly is the problem. Thanks
ayushgrga at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 18

> thankx abillconsl for your reply,its really so sweet

> of you but i still get some compile error and i am

> still trying to figure out the actual problem..

> thankx a lotz for your help i really appreciate

> you....but am still gonna check

Did you notice that there was a bit of code that abillconsl left out? There was a very helpful link in there somewhere, too...

kevjavaa at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 19
yes i checked the link and the code tooAs i am a first year IT student current involving in java 0the code mentioned here are totally new for me so still cant figure it outthanks
ayushgrga at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 20

Okay, tell us, what are your specifica questions? Where are you stuck.

If it's the algorithm, I can't do better than the site I posted - perhaps someone else can, but I havn't been doing "math" of this kind for a good long time.

If it's question of implementing the algorithm (or translating it from the snippets shown) try [url Euclidean algorithm in java]this [/url]link.

If it's a matter of not understanding some of the other code, ask specific questions and for greater depth read the tutorials @java.sun.com.

~Bill

abillconsla at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 21
eheh thanksi think i am irritating u a lotzwell i just need a code to check whether the given two numbers like 6,35 is relative prime or not if not it will display the biggest common factorsorry for troubling youthankx
ayushgrga at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 22
So you want someone to give you the code without learning anything yourself.Good luck on that.
Aknibbsa at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 23

> eheh thanksi think i am irritating u a lotz

>

> well i just need a code to check whether the given

> two numbers like 6,35 is relative prime or not if

> not it will display the biggest common factor

>

You are wrong. You don't need the code you need the understanding so you can write the code. No one here should just give you the code. That is cheating and would deprive you of learning. Go back to the links and study some more. If you need help understanding a point or two, then just ask the specific point you don't understand. We'll be glad to assist as best we can. If you just want the code, then go somewhere else.

Message was edited by:

petes1234

petes1234a at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 24

> well i just need a code to check whether the given

> two numbers like 6,35 is relative prime or not if

> not it will display the biggest common factor

You've already said this a few times. Repeating it over and over doesn't cause it to convey any additional information. You're not answering the questions others and I have asked.

jverda at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 25
Look more carefully at post #20 ... it's a link to the code. You can actually get the code and paste it in.
abillconsla at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 26
How you do'n with it?
abillconsla at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 27
well honestly speaking i am still stucked n i dont get a thingas i am just a beginner its really difficulti know its cheating but its just a practice exercise that i wanna do i tried all n nothing is working thnks a lots allz
ayushgrga at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 28

> well honestly speaking i am still stucked n i dont

> get a thing

> as i am just a beginner its really difficult

>

> i know its cheating but its just a practice exercise

> that i wanna do i tried all n nothing is working

>

> thnks a lots allz

1) if it's for practice means you don't have to do it

2) cheating means you learn nothing

So either don't do it and don't care as it's practice or you are lying and it's worth marks.

Aknibbsa at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...
# 29

Well as we've said, we can help, but we'd need to know what you need help with. Ask specific questions ... even if it's just one at a time. The day is ticking off and many of us are or will soon be, on our way home from work or school and that will likely be that until Monday. So if you have questions, now is the time to start asking them.

abillconsla at 2007-7-21 22:07:32 > top of Java-index,Java Essentials,Java Programming...