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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# problem in prime spiral

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.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: 13089
67
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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: 79273
377
• Number of slices to send:
Optional 'thank-you' note:
Please search for a description of a prime spiral, or ask your teacher to explain it to you.

 And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com