Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sorry I forgot to link the other problem as well Problem 1: Back in 1963, while

ID: 3813519 • Letter: S

Question

Sorry I forgot to link the other problem as well

Problem 1: Back in 1963, while doodling during a boring talk at a scientific meeting, a Polish-American mathematician named Stanisla came up with what known as the Prime Spiral. While drawing a grid of lines, he decided to number the intersections according to spiral pattern as you see in the examples. He then began ling the numbers in the spiral that were primea. Surprisingly, the circled primes appeared to fall along a number of diagonal straight lines. 17 16 15 14 13 Prime Spiral of Size N-5 Prime Spiral of Size N-3 In this problem, we are interested in finding the largest sum of primes along any diagonal straight line for any given grid of size N 100. For example, inspecting the prime spiral for N-5, the largest sum of primes on diagonal line is 9 +7+23 -49. Similarly the largest sum of primes forN 3 is 10. nput The first line in the input file ('spiral in contains a single integer D, which represents how many data sete are used to test your program. Each data set contains exactly one integer, N,representing the size of the grid, on a separate line. Nis always an odd number in the range 0

Explanation / Answer

1.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo {
public static void main(String[] args) throws IOException {
  BufferedReader br = new BufferedReader(new FileReader("spiral.in"));
  BufferedWriter bw = new BufferedWriter(new FileWriter("spiral.out"));
  String thisLine = "";
  int countOfLine = 0;
  int noOfSet = 0;
  while ((thisLine = br.readLine()) != null) {
   if (countOfLine == 0) {
    noOfSet = Integer.parseInt(thisLine);
   } else {
    for (int idx = 0; idx < noOfSet; idx++) {
     int n = Integer.parseInt(thisLine);
     int A[][] = new int[n][n];
     int k = n * n, c1 = 0, c2 = n - 1, r1 = 0, r2 = n - 1;

     while (k >= 1) {
      for (int i = c2; i >= c1; i--) {
       A[r2][i] = k--;
      }

      for (int j = r2 - 1; j >= r1; j--) {
       A[j][c1] = k--;
      }

      for (int i = c1 + 1; i <= c2; i++) {
       A[r1][i] = k--;
      }

      for (int j = r1 + 1; j <= r2 - 1; j++) {
       A[j][c2] = k--;
      }

      c1++;
      c2--;
      r1++;
      r2--;
     }

     /* Printing the Circular matrix */
     System.out.println("The Circular Matrix is:");
     for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
       System.out.print(A[i][j] + " ");
      }
      System.out.println();
     }
     /*
      * int maxSum = 0; for (int l = 0; l < n * 2; l++) { int sum
      * = 0;
      *
      * for (int j = 0; j <= l; j++) { int i = l - j; if (i < n
      * && j < n) { if (isPrime(A[i][j])) { sum = sum + A[i][j];
      * } } } if (sum > maxSum) { maxSum = sum; } }
      */
     int maxSum = findMaxSum(A, n);
     bw.write("the maxSum of Prime is " + maxSum);
    }
   }
  }
  br.close();
  bw.close();

}

static int findMaxSum(int A[][], int n) {
  int maxSum = 0;
  for (int l = 0; l < n * 2; l++) {
   int sum = 0;

   for (int j = 0; j <= l; j++) {
    int i = l - j;
    if (i < n && j < n) {
     if (isPrime(A[i][j])) {
      sum = sum + A[i][j];
     }
    }
   }
   if (sum > maxSum) {
    maxSum = sum;
   }
  }
  return maxSum;

}

static boolean isPrime(int n) {
  if (n == 1) {
   return false;
  }
  for (int i = 2; i < n; i++) {
   if (n % i == 0)
    return false;
  }
  return true;
}

}

2.

import java.util.ArrayList;
import java.util.List;

class Item {
private String itemName;
private double itemPrice;

public Item() {

}

public Item(String itemName, double itemPrice) {
  super();
  this.itemName = itemName;
  this.itemPrice = itemPrice;
}

public String getItemName() {
  return itemName;
}

public void setItemName(String itemName) {
  this.itemName = itemName;
}

public double getItemPrice() {
  return itemPrice;
}

public void setItemPrice(double itemPrice) {
  this.itemPrice = itemPrice;
}

@Override
public boolean equals(Object arg0) {
  Item i = (Item) arg0;
  if (this.itemName.equals(i.getItemName())) {
   return true;
  } else {
   return false;
  }
}

@Override
public int hashCode() {
  return this.itemName.hashCode() * 4 + 325;
}

@Override
public String toString() {
  return "Item [itemName=" + itemName + ", itemPrice=" + itemPrice + "]";
}

}

public class Shoppinglist {
private static List<Item> listOfItems = new ArrayList<Item>();

public static void addToStart(String itemName, double itemPrice) {
  listOfItems.add(new Item(itemName, itemPrice));
}

public static boolean deleteItem(int pos) {
  int previousSize = listOfItems.size();
  listOfItems.remove(pos);
  int newSize = listOfItems.size();
  return (previousSize - newSize) == 1 ? true : false;
}

public static int getSizeofList() {
  return listOfItems.size();
}

public static double totalPriceInList() {
  double sum = 0;
  for (Item i : listOfItems) {
   sum = sum + i.getItemPrice();
  }
  return sum;
}

public static int findItem(String itemName) {
  Item findItem = new Item();
  findItem.setItemName(itemName);
  if (listOfItems.contains(findItem)) {
   return 1;
  } else {
   return 0;
  }
}

public static void outputList() {
  for (Item i : listOfItems) {
   System.out.println(i);
  }
}

}

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
  Scanner s = new Scanner(System.in);

  while (true) {
   System.out.println("1 QUIT");
   System.out.println("2 OUTPUT THE LIST");
   System.out.println("3 ADD AN ITEM");
   System.out.println("4 DISCARD AN ITEM");
   System.out.println("5 FIND ITEM");
   System.out.println("6 HOW MUCH I HAVE SPENT");
   System.out.println(" ");
   System.out.println("Please enter your selection");
   int option = s.nextInt();
   if (option == 1)
    break;
   else if (option == 2) {
    Shoppinglist.outputList();
   } else if (option == 3) {
    Shoppinglist.addToStart("Mobile", 23540.25);
   } else if (option == 4) {
    Shoppinglist.deleteItem(1);
   } else if (option == 5) {
    System.out.println(Shoppinglist.findItem("Mobile"));
   } else if (option == 6) {
    System.out.println(Shoppinglist.totalPriceInList());
   } else {
    continue;
   }
  }
  s.close();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote