For some reason, this code keeps giving me wrong answers, which I went through and got the right prime numbers up to 200.

import java.io.*;
import java.util.Scanner;
public class Main
{
	private static int from;
	private static int to;
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int times = sc.nextInt();
		for (int i = 0; i < times && times > 0; i++)
		{
	  		from = sc.nextInt();
	  		to = sc.nextInt();
	  		while (from < to)
	  		{
	  			if (from==2 || from==3 || from==5 || from==7 && from!=1)
	  				System.out.println(from);
	  			if (from%2!=0 && from%3!=0 && from%5!=0 && from%7!=0 && from!=1)
	  				System.out.println(from);
	  			from++;
	  		}
		}
	}
}
  • created

    Oct '09
  • last reply

    Oct '09
  • 1

    reply

  • 153

    views

  • 2

    users

There isn't anything special about the numbers 2, 3, 5 or 7 - what about 13*13? Or some other number with prime factors bigger than 7?

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.