Question

Write Java program that takes an integer N from the command line and creates N by...

Write Java program

that takes an integer N from the command line and creates N by N boolean array a[][] such that a[i][j] is true if i and j are relatively prime (have no common factors) and false otherwise. print the output using * to represent true and a space to represent false and include row and column numbers.

(use sieving)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class RelativePrime {

    public static boolean areRelativePrimes(int n1, int n2) {
        for (int i = 2; i < Math.min(n1, n2); i++) {
            if (n1%i == 0 && n2%i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        if (args.length > 0) {
            int n = Integer.parseInt(args[0]);
            boolean[][] arr = new boolean[n][n];
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    arr[i][j] = areRelativePrimes(i, j);
                }
            }
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i][j]) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        } else {
            System.out.println("Please provide a number from command line arguments");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write Java program that takes an integer N from the command line and creates N by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
Active Questions
ADVERTISEMENT