Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

the isPrime method in java.

Name: Anonymous 2006-10-17 1:20

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.

Name: Anonymous 2006-10-17 7:52

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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List