• 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

Need help with Prime number Loop and with boolean statement

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me where I'm going wrong here in making this read all prime numbers
import java.util.*;

Here is my original code


Here is the code where I was asked to combine a while loop with a boolean statement. I need help on both.



[Edit: put code between [code] and [/code] ubb code blocks]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first class:
  • What is the definition of a Prime number? How are you handling the two conditions to validate that it is a prime?
  • What are you trying to do in lines 20 - 22?

  •  
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are both mistaken using ½ the number as the limit for testing whether it is prime. You ought to use √number. You also should use a Sieve of Eratosthenes to populate a boolean array and that will tell you whether a number is prime or not. Much faster and efficient than using the % operator. If you look in the Wikipedia article which appears from that link, there is the Sieve of Euler, too.
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    vinayak jog - thank you for replying, however at CodeRanch we work on the theory that we want to teach people how to solve the problems themselves. Complete solutions don't lend themselves to that - the learning experience is removed.

    I therefore removed your response.
     
    Do not set lab on fire. Or this 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