Return Entire Thread First 100 Posts Prev 100 Next 100 Last 50 Posts Report Thread Style: Yotsuba, Pseud0ch, Terminal

Pages: 1-

the isPrime method in java.

1

Write a method name isPrime, Which takes an integer as an argument and returns true if the argument is a prime number,or false otherwise.

what i dont understand about what it is asking me to do though is whether i should actually type in a random number while the program is running or should i have it read from a simple txt. file. and if i do have to type in a random number lets say 5 it would return true, but am i supposed to program that 5 into the code for it to be able to recognize it as a prime number or is that already determinded with the isPrime method?

I started learning Java only recently... so far coding has interested me a lot but the way that java fuckin does things is just so... fuckin annoying... i now understand why you guys flame it so much sometimes.

any advice on what i should do about this java thing would be really appreciated.

2

FUCK YOU, 2 IS A FUCKING PRIME

3

so far i have something like this... am i doin it right?

import java.io.*;
      public class Prime{
           
         public static void main(String[] args) throws Exception, IOException
             {
            InputStreamReader a= new InputStreamReader(System.in);
            BufferedReader stndin= new BufferedReader(a);
            boolean isprime=true;
             String snum;
             int inum;
             System.out.println("Enter a number:  ");
             snum=stndin.readLine();
             inum=Integer.parseInt(snum);

             int j=inum%2;

            
              if (j==1){
              System.out.println("prime ");
              }else{
              System.out.println("composite");}
       
     
           
         }
}

4

First off are you even aware of the mathematical theories behind finding out if a number is prime or not?

And no, >>3 looks like all it does is find out if a number is odd or even.  You CAN have it immediately return false if it's even, since all even numbers are composite.

Otherwise, I'm not too knowledgable on this specific thing myself, but I'm pretty sure this falls into the realm of Discrete Mathematics, as it's called in college.  Obtain a textbook or Google it and understand the math before you touch your keyboard again.

5

>>1
and if i do have to type in a random number lets say 5 it would return true, but am i supposed to program that 5 into the code for it to be able to recognize it as a prime number or is that already determinded with the isPrime method?
This thread is LOL. Anonymous doesn't even know what a prime number is.

Trust me pal, as much as Java sucks, the problem is not Java.

6

>>5
is 5 not a prime number though?

7

DO YOUR OWN HOMEWORK FAGGOT

8

Here's a prime function I had to do for a C++ assignment.

int prime (int numberToTest)
{
    if (numberToTest > 1)
    {
        int divideLimit = numberToTest/2;
        int divideCurrent = 2;
       
        while (divideCurrent <= divideLimit)
            if (numberToTest % divideCurrent++ == 0)
                return 0;
           
        return 1;
    }  
   
    return 0;
}

9

Oh BTW, the assignment specs said 1 should NOT be treated as a prime for some reason.

10

Lol, this is why Java fails hard. A method? Method of what? You cannot modify or even inherit from int, and Integer is useless. You have to make it a method because the stupid language doesn't have functions. You need to create a useless class made of entirely static methods (breaking OO beacuse it's not actually a class), or you have to create a class which needs instancing and holds state. Either way you end up with a stupid useless mess you need to document and programmers need to learn about, when all they needed is an IsPrime(p) function.

Anyways, you don't need to be Einstein to tell whether a number is a prime. This is the algorithm in a decent language:

import math
def IsPrime(p):
    if p < 0: p = -p
    if p < 2: return False
    max = int(math.sqrt(p))
    for i in xrange(2, max + 1): #C eqv: for (i = 2; i <= max; i++)
        if p % i == 0:
            return False
    return True

11

Sieve of Erastothenes ftw

12

>>9

That's because it isn't prime.

13

I also remember some algorithm I "accidentally" learned in a computer security course where we had to hand-compute whether or not a number was prime, as a sub-part of encoding/decoding plaintext with the RSA algorithm.  It wasn't the algorithm in >>10, but it was just as good.  It involved searching for the prime factors of a number and something like that, like after the loop was over if some variable was equal to 1 then the number was prime.  I wish I hadn't tossed all my notebooks in the trash.

14

just use Fermat's little theorem.

Not 100% but works well if u use a prime for the moduolo (a large prime).

It doesnt incur the large x^2 execution time (optimal) of a seive , esp for large numbers.

Tho this comes at the expense of accuracy, some numbers it is unable to ID as prime or not.

15

>>11
Sieve of Erastothenes for the win of RAM business

16

Win32api is deprecated

18

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

19


I liked the atmosphere of couple first episodes very much, then there were many rather uninteresting plot developments but overall very feel giving and satisfying experience.

20


A neutron bomb is the opposite of what you want for this sort of thing. The point of a traditional fission or fusion bomb is to release great energy in the form of physical shocks and heat to primarily cause burning and overpressure damage to targets.

21


Teach a man to fish, and you just eliminated your customer base idiot. Their ignorance was your job security as a restaurant owner ! Now you have to face a foreclosure! Are you proud of yourself? Are you?


Newer Posts

Name: Email:
22
Entire Thread Last 50 Posts First 100 Posts Thread List Report Thread