• 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 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 numbers using an array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm designing a program to generate and calculate first 100 prime numbers according to the following:

The Intbag class holds the values of the collection. Through that class, i will calculate the first 100 prime numbers.

To generate the prime numbers, i want to divide each number by the prime numbers in the collection.

Here is the IntBag class :



Here is my main method :



But the program only generates the first 2 prime numbers(2 and 3). I tried many times but couldnt figure out why.?
can you help me?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you got that for loop with no continuation condition? Why have you nested it inside a while loop? Why do you have a constructor allowing you to set the size of your array and then test it against 100? You have a serious design flaw in the get(int) method; what happens if you have a 50-element array and look for no 60?

How are you calculating prime numbers? You ought to write it down on paper and get an algorithm working. Those three nested loops look very complicated to me, and suggest to me there is a better way to so that.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alper cem polat wrote: . . . The Intbag class . . . Through that class, i will calculate the first 100 prime numbers. . . .

No, you aren’t. You are using something else to try and calculate prime numbers.

You don’t think a Sieve of Eratosthenes would be better?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Welcome to The Ranch!

Have a closer look at your prime variable. You initialise it to true, it gets set to false the first time you find a prime factor...and it never gets changed again.

Your combination of a while and for loop on lines 19 and 20 looks strange as well - all you're wanting to do is increment a until primes is full, so you only need one loop for that. It's not clear at the moment how the inner loop is ever going to end.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell's also right about the Sieve of Eratosthenes if you're looking for a good but simple way to generate primes (although in its simplest form that generates primes up to a particular value rather than a particular number of them).
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic