473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

urgent sieve of eratossthenes prime #'s

alright i need some help making a program that displays all the prime number from 1-1000. i can make one but i cant make one with arrays. need help!
Feb 8 '07 #1
3 1072
horace1
1,510 Expert 1GB
alright i need some help making a program that displays all the prime number from 1-1000. i can make one but i cant make one with arrays. need help!
The experts on this site are more than happy to help you with your problem but we cannot do your assignment for you. Attempt the assignment yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Feb 8 '07 #2
this is what i have done

package prime;

public class Prime {
public static void main(String[] args) {
int i,j,p;
for(i=2;i<=1000;i++)
{
p = 0;
for(j=2;j<=i/2;j++)
{
if(i%j == 0)
{
p = 1;
break;
}
}
if(p == 0)
System.out.println(i);
}
}

}

it works, however i need to have it as arrays, which i am unable to do...could i possibly get help on this??
Feb 8 '07 #3
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import static java.lang.Math.*;
  4. public class testPrime{
  5.  public static void main(String[] args) {
  6.   int limit = 7000;
  7.   int i,j;
  8.   int[] intervals = new int[7];
  9.   for(int h = 0; h < intervals.length; h++) {
  10.    intervals[h] = 0;
  11.   }
  12.   boolean []prime = new boolean[limit+1];
  13.   for(i = 2; i <= limit; i++) {
  14.    prime[i] = true;
  15.    int max = (int)sqrt(i) + 1;
  16.    for(j = 2;j < max; j++){
  17.     if((i % j) == 0) {
  18.      prime[i] = false;
  19.  
  20.     } 
  21.  
  22.    }
  23.    if(prime[i] == true) {
  24.     if(i <= 1000) {
  25.      intervals[0]++;
  26.     }
  27.     else if(1000 < i && i <= 2000) {
  28.      intervals[1]++;
  29.     }
  30.     else if(2000 < i && i <= 3000) {
  31.      intervals[2]++;
  32.     }
  33.     else if(3000 < i && i <= 4000) {
  34.      intervals[3]++;
  35.     }
  36.     else if(4000 < i && i <= 5000) {
  37.      intervals[4]++;
  38.     }
  39.     else if(5000 < i && i <= 6000) {
  40.      intervals[5]++;
  41.     }
  42.     else if(6000 < i && i <= 7000) {
  43.      intervals[6]++;
  44.     }
  45.     //System.out.println(i+\" is_prime**\");
  46.    }
  47.    else {
  48.     //System.out.println(i+\" no_prime\");
  49.    }
  50.   }
  51.   System.out.println(\"Highest  interval \" + testPrime.dense(intervals) + 1);
  52.  
  53.   int scale = 110;
  54.   int n = 1000;
  55.   for(int k = 0; k < intervals.length;k++) {
  56.  
  57.    System.out.print((n - 999) + \"-\" + n );
  58.    if(k == 0) {
  59.     System.out.print(\"   \");
  60.    }
  61.    System.out.print(\" :\");
  62.    for(int l = 0; l < intervals[k] - scale; l++) {
  63.  
  64.     System.out.print(\"*\");
  65.    }
  66.    System.out.println(\"\"+intervals[k]);
  67.    n = n + 1000;
  68.   }
  69.  
  70.  }
  71.  public static int dense(int[] a) {
  72.   int highest = 0;
  73.  
  74.   for(int i = 1; i < a.length;i++) {
  75.    if(a[i] > a[highest]) {
  76.     highest = i;
  77.    }
  78.   }
  79.   return highest;
  80.  }
  81. }
  82.  
  83.  
Feb 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: MSiegel | last post by:
hi there! i have to program the sieve of eratosthenes in php as a homework. after i had created an html file where the maximum is set i wrote a php script which doesn't work properly - actually...
0
by: Mark A. Washburn | last post by:
/* SIEVE OF ERATOSTHENES from BYTE magazine -------------------- -- compiled with jdk 1.1.7b with optimize on ( -O) -- Run times on 300 MHz Pentium 2 Windows 95 -- in order of output, from...
36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
15
by: Steve Bergman | last post by:
Just wanted to report a delightful little surprise while experimenting with psyco. The program below performs astonoshingly well with psyco. It finds all the prime numbers < 10,000,000 ...
4
by: knuxus | last post by:
Hey everyone.... I would appreciate any type of help if anyone could explain me how to translate this algorithm to Visual Basic, im still learning and i would appreciate this algorithm to my prime...
2
Blackout
by: Blackout | last post by:
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of...
3
by: jzakiya | last post by:
This is to announce the release of my paper "Ultimate Prime Sieve -- Sieve of Zakiiya (SoZ)" in which I show and explain the development of a class of Number Theory Sieves to generate prime...
4
by: jzakiya | last post by:
Update: 2008/11/03 Architecture & coding improvements. Renamed generators. I am 90% finished writing up a mathematical analysis of my method. In the process I found an architectural...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.