Infinite loop when printing an N x N table - java

Consider the following Java program:
public class RelativelyPrime {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]); // Dimensions of grid
int i, j;
int r; // Remainder when i is divided by j
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
do { // Using Euclidean algorithm
r = i % j;
i = j;
j = r;
} while (r > 0);
if (i == 1) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
}
}
This program prints an N x N table (or matrix, if you like) where N is a command-line argument.
The (i, j)-entry is a * if i and j are relatively prime, or a single whitespace if they are not relatively prime. When I run the program by entering, for instance, java RelativelyPrime 3 it endlessly prints *. Why is this happening?

You changed i and j in the while loop.
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
int ii = i, jj = j;
do { // Using Euclidean algorithm
r = ii % jj;
ii = jj;
jj = r;
} while (r > 0);
if (ii == 1) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}

This is where using the debugger would have helped you solve the problem.
Inside your loops, you alter both i and j which means they never reach N and thus you have an infinite loop.
I suggest you not alter these variables but instead use two new variables, ideally with meaningful names.

Related

Need help optimizing Java program for Goldbach Conjecture

I need to print all prime numbers from 1 to 1,000,000 and print all even numbers from 4 to 10,000 and two prime numbers that sum to it.
I have a sieve method that changes all non-prime numbers in an array to a 0 (the problem specifically asks for this to be done), and I need to use a goldbach method that passes this array and displays all even numbers from 4 to 10,000 and two primes that sum up to that number.
The point of the goldbach portion of the problem is to print the numbers efficiently, and I am pretty sure my solution uses a polynomial time search when the correct solution is to be done with a linear time search. Any clue on how I might optimize this?
import java.lang.Math;
public class sieveAndGoldbach {
public static void sieve(int[] a) {
int n = a.length;
a[0] = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
if (a[i] != 0) {
for (int j = a[i]*a[i]; j <= n; j+=a[i]) {
a[j-1] = 0;
}
}
}
}
public static void goldbach(int[] a) {
int max = 10000;
for (int i = 4; i <= max; i += 2) {
int count = 0;
for (int j = 0; j < i/2; j++) {
if (a[j] != 0) {
int difference = i-a[j];
for (int k = 0; k < max; k++) {
if (a[k] == difference && count == 0) {
System.out.println(i + " = " + a[j] + " + " + (difference));
count++;
}
}
}
}
}
}
public static void main(String[] args) {
//initialize and fill array from 1 to n
int n = 1000000; //initially one million GOLDBACH METHOD WILL NOT WORK FOR n < 10,000
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
//Call sieve method on array a, then print all primes, not the zeros
sieve(a);
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
System.out.print(a[i]);
System.out.print(" ");
}
}
System.out.print("\n");
//Call goldbach method on array a
goldbach(a);
}
}
You currently seem to be iterating through the array of primes for each prime looking for one that sums to your target. That's not necessary; you just need to check whether the difference is a prime:
int[] primes;
int target;
for (int i = 2; i < target / 2; i++) {
if (primes[i] != 0 && primes[target - i] != 0)
...
}
Beyond that I can't see a lot of obvious optimisation but there may well be some numerical analysis that allows you to target likely primes first.

How can we generate all submatrix of a given 2D matrix in java or C++?

I am using this loop structure but it fails to generate all the submatrix that are possible for any given 2D matrix with n rows and m columns.
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
System.out.println("sub-MATRIX:");
for(k=i;k<n;k++)
{
for(p=j;p<m;p++)
{
System.out.print(arr[k][p]+" ");
}
System.out.println();
}
}
}
Ex: Given matrix 3X3 : [[1 2 3],[4 5 6],[7 8 9]]
Then its submatrix will be:
for size 1:
[1],[2],[3],[4],[5],[6],[7],[8],[9]
for size 4:
[[1,2],[4,5]],[[2,3],[5,6]],[[4,5],[7,8]] and [[5,6],[8,9]]
and so on
You are missing a couple more loops to cover all cases. PrintMatyrix() should have 2 nested loops for printing contents.
for (i = 1; i < n; ++i)
{
for (j = 1; j < m; ++j)
{
// we are at each sub matrix of size(i,j)
for (k = 0; k <= (n - i); ++k)
{
for (p = 0; p <= (m - j); ++p)
{
// we are at submatrix of size(i,j) starting at (k,p)
// assuming PrintMatrix(Matrix&, int rows, int cols, int r0, int c0);
PrintMatrix(arr, i, j, k, p);
}
}
}
}
If we have a matrix with dimensions M x N, and the sub matrix we are looking for is with K x L dimensions. If there is more optimized solution, please share.
for (int i = 0; i < m-k+1; i++) {
for (int j = 0; j < n-l+1; j++) {
for (int p = 0; p < k; p++) {
for(int q = 0; q < l; q++) {
System.out.print(arr[i+p][j+q] + " ");
}
System.out.println();
}
System.out.println("*****************");
}
}
You should not loops, you should use some recursions.
Think about this way, for each row or column, you either take it or throw away it. So you can select rows first and then columns and based on the rows and columns selected you construct the submatrix.
Some code,
bool rowTaken[N], columnTaken[M];
void constructSubMatrixRow(int i)
{
if (i >= N) constructSubMatrixCol(0);
rowTaken[i] = true;
constructSubMatrix(i+1);
rowTaken[i] = false;
constructSubMatrix(i+1);
}
void constructSubMatrixCol(int i)
{
if (i >= M) printSubMatrix();
columnTaken[i] = true;
constructSubMatrixCol(i+1);
columnTaken[i] = false;
constructSubMatrixCol(i+1);
}
void printSubMatrix()
{
for (unsigned i = 0; i < N; i++)
if (rowTaken[i]){
for (unsigned j = 0; j < M; j++)
if (columnTaken[j])
print matrix[i][j]
}
}

using java for loop with user input to make custom diamond

I am looking to make a diamond like this:
n=2
*
*$*
*
n=3
*
*$*
*$*$*
*$*
*
n=4
*
*$*
*$*$*
*$*$*$*
*$*$*
*$*
*
I can get the diamond with just * but cannot figure out how to add the $ in the mix
My code is as follows:
import java.util.Scanner;
public class ForNestedDemo
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please input number of lines:");
int i = 0, j, k, n;
n = scan.nextInt();
for (k = 1; k <= (n + 1) / 2; k++) {
for (i = 0; i < n - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("*$ ");
}
System.out.println("");
}
for (k = ((n + 1) / 2); k < n; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < n - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
scan.close();
}
}
I agree that #GhostCat is the easiest way to go, but just for fun I figured it out using your way.
for (k = 1; k < (n + 1); k++) {
for (i = 0; i < n - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
if(j == 0)
if(k == n+1)
System.out.print("*");
else
System.out.print(" *");
else{
System.out.print("$*");
}
}
System.out.println("");
}
for (k = 1; k < n; k++) {
for (i = 0; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < n - k; j++) {
if(j == 0)
if(k == n+1)
System.out.print("*");
else
System.out.print(" *");
else{
System.out.print("$*");
}
}
System.out.println("");
}
I have fixed some of your errors and added some checks in there also.
The logic I have in place is:
If you are the first character, are you the middle row (k == n+1), if so, only print *, otherwise print _*.
If you are not the first character, print $*.
After that I just simply took my logic and pasted it down below in your lower half loop.
A simply way would be: instead of directly printing those "patterns", push them into string variables first - without thinking about $ signs. Just put the required spaces and *s into those strings.
Like:
" *"
" **"
"***"
Then, take those strings, and build the final strings from them: walk through each string, and when you find that str[index] and str[index+1] are '*' you simply put "*$" into your result string (otherwise you just copy the character at index).
Using that algorithm, the above strings turn into
" *"
" *$*"
"*$*$*"
And finally, you simply *copy** the upper lines down!
For the record: of course there are easy solutions that create the whole output in one shoot. But: you already got the loops in place that build lines that just miss the $ chars. So you can use my approach go get from your current code to a working solution easily.
You could do it as follows:
1. first print spaces
2. then print alternate '*' and '$' as per the order of the line.
public void printDiamonds(int n) {
//print the top part
for (int i = n-1; i > 0 ; i--) {
printDiamondLine(n, i);
}
//print the bottom part
for (int i = 0; i < n; i++)
printDiamondLine(n, i);
}
private void printDiamondLine(int n, int i) {
// i denotes the number of preceding spaces per line
for (int j=i; j>0; j--)
System.out.print(" ");
// print alternate * and $
for (int k=2*(n-i)-1; k>0; k--)
if (k%2==0)
System.out.print("$");
else
System.out.print("*");
System.out.println(); //print a new line at the end
}
Since this seems like homework, I normally wouldn't give the full code. But, the cat's out of the bag.
I would try to think of things in a simple mathematical manner and carefully look at the relationships between spaces, *s, and $s. When you do the code may come a little easier to write, simple, and cleaner.
There are always as many *s in a row as the row number
There are always one less as many $s as there are *s
There are always n - rowNum spaces in a row precedings the first character
There is a top, middle, and bottom to the shape based on these characteristics
Given that, I would start by writing a solution that would be easy to debug and very clean. The interesting part of my main would reflect the characteristics of the shape listed above:
printTop(1, 1, n);
printMiddle(n);
printBottom(n - 1, n - 1, n);
These methods would be defined as follows:
public static void printTop(int i, int j, int n) {
for (; n - j > 0; ++i, ++j) {
printLine(n - j, i);
}
}
public static void printMiddle(int stars) {
printLine(0, stars);
}
public static void printBottom(int i, int j, int n) {
for (; i >= 0; --i, --j) {
printLine(n - j, i);
}
}
The printLine() method prints a line/row of the shape given a number of spaces and *s. This is the part I would normally leave out but...
public static void printLine(int spaces, int stars) {
printSpace(spaces);
for (int i = 1; i <= (2 * stars - 1); ++i) {
if (i % 2 == 0) {
System.out.print('$');
} else {
System.out.print('*');
}
}
System.out.println();
}
I'm certain you can figure out what printSpace() is doing.
The benefit of this approach is it leads you towards what is inevitably going to be a primary goal of yours: decompose and modularize your code. This will become increasingly important as solutions become more complex. Good luck.
public static void main(String[] args) {
int n = 2;
int mid = (int) Math.ceil((n+n-1)/2.0);
String[] stringArray = new String[n];
for(int i = 0 ; i < n ; i++) {
StringBuilder sb = new StringBuilder();
for(int j = 2*i+1; j > 0; j--) {
if(j%2 == 0)
sb.append("$");
else
sb.append("*");
}
stringArray[i] = sb.toString();
}
for(int i = 0 ; i < n ; i++) {
for(int j = mid - stringArray[i].length()/2; j >0 ;j--) {
System.out.print(" ");
}
System.out.print(stringArray[i] + "\n");
}
for(int i = n-2 ; i >= 0 ; i--) {
for(int j = mid - stringArray[i].length()/2; j >0 ;j--) {
System.out.print(" ");
}
System.out.print(stringArray[i] + "\n");
}
}
Here you go. This code is not optimized in any way. Hope this gives you a general idea.

Java - Hourglass

I'm missing by just a little bit. What I want:
*******
*****
***
*
***
*****
*******
What I'm getting
*******
*****
***
*
*
***
*****
*******
The code
public class HD404 {
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 1;
for (int i = 0; i < N; i++) {
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
}
Right now I'm mostly just guessing and I just can't pin point my error. What am I missing here?
The problems lays with the second part of your code where you ask to draw one star and you start at zero where you should start at one.
Solution
x = 1;
for (int i = 0; i < N; i++)
should be replaced with
x = 3;
for (int i = 1; i < N; i++)
The problem is that you are starting to draw the bottom of the hourglass with 1 asterisk (x = 1) instead of 3.
The second issue is that the bottom of the hourglass only has N-2 lines, not N-1 so the loop should start at 1 instead of 0. This is because the line with a single asterisk was already drawn in the upper-half.
Corrected code:
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 3; // <-- not 1 here, the first line has 3 asterisks
for (int i = 1; i < N; i++) { // <-- i starts at 1 because the first line was already drawn in the upper half
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
As a side-note, you could rewrite this code a lot shorter by making the following observations:
There are x lines to draw so we can loop from 0 to x included (to respect the symmetry) and skip the middle line so as not to draw it twice
For every line, there are x columns to draw and it is either a space or a *.
For every line, * is drawn only if the current column is between min(i, x-i) and max(i, x-i) (if we're in the upper-part, i < x-i and if we're in the bottom-part, i > x-i).
Code:
public static void main(String[] args) {
int N = 4;
int x = 2 * N - 1;
for (int i = 0; i <= x; i++) {
if (i == N) continue; // skip the middle-line for it not to be drawn twice
for (int j = 0; j < x; j++) {
System.out.print(j >= Math.min(i, x-i) && j < Math.max(i, x-i) ? "*" : " ");
}
System.out.println();
}
}
Sample output:
*******
*****
***
*
***
*****
*******
The easiest way I can think up is probably to prevent the last iteration of your first outer loop, that way you'll prevent the first single star line to be shown.
I would probably do it this way:
for(int i = 0; i < N && x > 1; i++)
{
/*Code of the first inner loop*/
}
For those whose still looking for a simpler and lesser code regarding hourglass challenge. This contains 2 for loops only.
You may use this as reference.
public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;
for(int i=0; i<dimension; i++) {
int actual = space;
for (int j=dimension; j > 0; j--) {
if(actual > 0) {
System.out.print(" ");
actual--;
}
else {
System.out.print("*");
if(stars==printed) {
actual = space;
printed = 0;
} else {
actual = 1;
printed++;
}
}
}
if(i <= size-2) { // will pattern spaces and stars from top to middle
space++;
stars--;
}
else { // will pattern spaces and stars from middle to top
space--;
stars++;
}
System.out.println();
}
}

Printing a triangle in Java

I'm practicing basic coding exercises and trying to print the following triangle in Java:
*
***
*****
***
*
The following code gives me the results but I feel like there must be a much more elegant solution
for (int i = 1; i <= 5; i++) {
if (i % 2 == 1) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
for (int i = 3; i > 0; i--) {
if (i % 2 == 1) {
for (int j = 1; j < i + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
}
Can anyone provide some insight into how to make this work in a better way?
Ok, here's some more code that produces the correct result that uses just the two for loops, but it looks even uglier:
for (int i = 1; i <= 10; i += 2) {
if (i <= 5) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
else if(i > 5 && i < 8){
for(int j = i/2; j > 0; j--){
System.out.print("*");
}
System.out.println("");
}
else{
for(int j = 1; j > 0; j--){
System.out.print("*");
}
System.out.println("");
}
}
First, you are skipping each 2nd iteration of the loop because you want to increase two steps at once. You can do this by changing the "i++" in your loop to "i += 2" and "i--" to "i -= 2", that will have the same effect and allows you to remove the if inside both loops.
Another improvement would be using a single outer loop and figuring out whether the inner loop should be increasing or decreasing the amount of asterisks. Maybe you can come up with an equation that gives you the amount of asterisks based on the value of i? (I didn't want to solve it completely so you have some exercise left, just comment if you want a full solution)
Updated with a solution that might be considered elegant as you can change the height of the triangle and there is no repetition:
int height = 5;
for (int i = 1; i <= 2 * height; i += 2) {
int numAsterisks;
if (i <= height) {
numAsterisks = i;
} else {
numAsterisks = 2 * height - i;
}
for (int j = 0; j < numAsterisks; j++) {
System.out.print("*");
}
System.out.println();
}
What about the following?
public void printTriangle(int size) {
int half = size / 2;
for (int i = 0; i < size; i++) {
int stars = 1 + 2 * (i <= half ? i : size - 1 - i);
char[] a = new char[stars];
Arrays.fill(a, '*');
System.out.println(new String(a));
}
}
Or just a bit more optimized:
public void printTriangle(int size) {
int half = size / 2;
char[] a = new char[size];
Arrays.fill(a, '*');
for (int i = 0; i < size; i++) {
int stars = 1 + 2 * (i <= half ? i : size - 1 - i);
System.out.println(new String(a, 0, stars));
}
}
for(int i = 0; i < 7; i++) {
for(int j = 0; j < i; j++) {
print("*");
}
print("\n");
}
This can be another solution to print a regular right triangle...
Here's a different way of looking at the problem. By using an integer array, I can solve lots of shape drawing problems by changing the values in the array.
When solving more difficult problems, you would use model classes instead of simple integers. The idea, however, is the same.
Here's the output.
*
***
*****
***
*
And here's the code:
public class Triangle {
public static void main(String[] args) {
int[] heights = {1, 3, 5, 3, 1};
for (int i = 0; i < heights.length; i++) {
for (int j = 0; j < heights[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
How about...
int width = 5;
for (int i = 1; i <= width; i+=2){
System.out.println(String.format("%"+i+"s", "").replaceAll(" ", "*"));
}
for (int i = width-2; i > 0; i-=2){
System.out.println(String.format("%"+i+"s", "").replaceAll(" ", "*"));
}
Or, even better yet...
int width = 7;
double half = width / 2
for (int i = 0; i < width; i++){
System.out.println(String.format("%"+((i < half ? i : (width-i-1))*2+1)+"s", "").replaceAll(" ", "*"));
}
Gives
*
***
*****
***
*

Categories