This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

problem in prime spiral

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

iam new member in this website and my first topic

I have question in prime spiral
here is question :
Write a JAVA program that takes one or more values for N on the command line
where N is always an odd number in the range 0 < N < 100.
For each N, your program must determine the largest sum of primes along any
diagonal straight line in a grid of size NxN. Write the answer on a separate line,
in the form:
N: S
where N is the grid size, S is the largest sum. For example, given 3 and 5 on the
command line, your program should print:
3: 10
5: 49


also it has two files "spiral.in" which contain input (2,3,5),and "spiral.out" (49,10)
how I can write code for this question ???
plzz help me
 
lowercase baba
Posts: 12800
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step 1: turn off your computer
step 2: THINK through the problem. try and write out the steps YOU would take to work through it, if all you had were paper, pencil, your brain, and an eraser.
step 3: revise the above steps, making them simpler and clearer
step 4: Only when you completely understand how to do this problem in your brain should you consider writing a single line of java.

As Winston (I think it is Winston) says, programming 90% THINKING and 10% writing lines of code.
 
rose khf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is first step which is to cheak if number is prime or not using if statement [package primespiral;


import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Primespiral {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner infile = new Scanner(new FileReader("spiral.in"));
PrintWriter outfile = new PrintWriter("spiral.out");
//define limit
int size = 100;

//loop through the numbers one by one
for (int i = 0; i < 100; i++) {
boolean isPrime = true;
//check to see if the number is prime
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
// print the number
if (isPrime) {
outfile.print(i + " ");
}
}

}
}


]


then how I can found largest sum of primes in diagonal ???
 
fred rosenberger
lowercase baba
Posts: 12800
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rose khf wrote:then how I can found largest sum of primes in diagonal ???


How would you do it with pencil and paper?
 
rose khf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

rose khf wrote:then how I can found largest sum of primes in diagonal ???


How would you do it with pencil and paper?


I don't know you have to tell me otherwise why I put the question
 
Marshal
Posts: 68135
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search for a description of a prime spiral, or ask your teacher to explain it to you.
 
Hey cool! They got a blimp! But I have a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!