Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#One array solution

One array solution

The following algorithm is an improvement on the two array system stated bellow it. Basically, we only need the context of the old previous value (as well as the next value, which goes unmodified), so we copy this value. From there we can set the new end to 1 then start over. This cuts down on the memory overhead. I think you'll see minimal computational improvements as yours was already pretty efficient, but this one takes less memory. I suspect there will be a performance increase where you are dealing with pretty large arrays.

import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = 1;
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}

#One array solution

The following algorithm is an improvement on the two array system stated bellow it. Basically, we only need the context of the old previous value (as well as the next value, which goes unmodified), so we copy this value. From there we can set the new end to 1 then start over. This cuts down on the memory overhead. I think you'll see minimal computational improvements as yours was already pretty efficient, but this one takes less memory. I suspect there will be a performance increase where you are dealing with pretty large arrays.

import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = 1;
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}

One array solution

The following algorithm is an improvement on the two array system stated bellow it. Basically, we only need the context of the old previous value (as well as the next value, which goes unmodified), so we copy this value. From there we can set the new end to 1 then start over. This cuts down on the memory overhead. I think you'll see minimal computational improvements as yours was already pretty efficient, but this one takes less memory. I suspect there will be a performance increase where you are dealing with pretty large arrays.

import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = 1;
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}
deleted 809 characters in body
Source Link
Neil
  • 421
  • 3
  • 9

#Two array solution

import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row1 = new int[toNthLine+1];
 int[] row2 = new int[toNthLine+1];
 int counter = 0;
 row1[0] = 1;
 row2[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 for (int j = 0; row1[j+1] != 0; j++) {
 row2[j+1] = row1[j] + row1[j+1];
 if (row2[j+1] % 2 == 0)
 counter++;
 }
 row2[i] = 1;
 int[] temp = row1;
 row1 = row2;
 row2 = temp;
 }
 System.out.println(counter);
 }
}

#Two array solution

import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row1 = new int[toNthLine+1];
 int[] row2 = new int[toNthLine+1];
 int counter = 0;
 row1[0] = 1;
 row2[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 for (int j = 0; row1[j+1] != 0; j++) {
 row2[j+1] = row1[j] + row1[j+1];
 if (row2[j+1] % 2 == 0)
 counter++;
 }
 row2[i] = 1;
 int[] temp = row1;
 row1 = row2;
 row2 = temp;
 }
 System.out.println(counter);
 }
}
deleted 5 characters in body
Source Link
Neil
  • 421
  • 3
  • 9
import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = row[0];1;
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}
import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = row[0];
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}
import java.util.Arrays;
class Pascal{
 public static void main(String []args) {
 int toNthLine = 1000;
 int[] row = new int[toNthLine+1];
 int counter = 0;
 int previous;
 row[0] = 1;
 for (int i = 0; i < toNthLine; i++) {
 previous = 1;
 for (int j = 0; row[j+1] != 0; j++) {
 int temp = row[j+1];
 row[j+1] = previous + row[j+1];
 previous = temp;
 if (row[j+1] % 2 == 0)counter++;
 }
 row[i] = 1;
 }
 System.out.println(counter);
 }
}
added 706 characters in body
Source Link
Neil
  • 421
  • 3
  • 9
Loading
Source Link
Neil
  • 421
  • 3
  • 9
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /