Here's some pseudo-code that you might find useful:
//global variables
int tempNum; //holds number as it's read
int numPrime; //increment by 1 each time a prime is found
//loop to read in numbers until end of file is reached
while(file.hasNext()){
//read in number (integer in this example) and store in variable temporarily
tempNum = file.nextInt();
//conditional statement (if statement) to check for prime numbers
if(tempNum % 2 == 0){
//number is prime, since the number divided by 2 does not produce a remainder
//so we will increment the "numPrime" variable by 1
numPrime++;
}
}
Here is a link to the above pseudo-code on PasteBin.com, where it has proper indentation and syntax highlighting (for Java, but it won't make a difference in this case), which may be easier to read.
http://pastebin.com/HHD0j5ux
Best of luck and I hope I helped you!
You shouldn't ask compound questions like this.
Before you worry about if the numbers are prime, you should just get the list of number from the text file.
IF you already know how to do that, then you shouldn't have asked us about that.
IF you don't know how to do that, you shouldn't have bothered asking about the prime test.
Do you know how to read numbers from a text file? Will you be creating the text file first? You didn't say what the format of the file is.
I'd ask this question on stack overflow. Might be more helpful. But for an isPrime function:
http://stackoverflow.com/questions/15285...
This could be what you're after. The following code assumes you have a data file in the same directory ("MyDataFile.txt") with one number on each line. Good Luck!
#!/usr/bin/python
def isPrime(n):
.... if n<2:
.... ....return False
.... for i in range(2, n):
.... .... if n%i==0:
.... .... .... return False
.... return True
file = open("MyDataFile.txt") # put name of data file here
data = file.readlines()
for line in data:
.... if isPrime(int(line)):
.... .... print "Prime:",line,
file.close()
#Note: I've used "...." to represent tabs, which Y!Answers removes.
First of all, use an algorithm to identify if a number is prime or not. If you want just any algo, above answers are good to go. If you want an efficient algo, go with sieve of eratosthenes.