# How to check for prime and perfect numbers when given a .txt file?

Page 1 of 1

## 7 Replies - 912 Views - Last Post: 18 June 2012 - 12:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=283147&amp;s=5000e8fbb7ed8e87764ba6dcc6c208de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Beastleewr11298

Reputation: 0
• Posts: 55
• Joined: 13-June 12

# How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 09:52 AM

Hello I am having trouble figuring out how to check for a prime and perfect number when given a .txt file with only 2 numbers. The .txt file would include, for example 2 100. So my program would have to scan between 2 and 100 but I am not sure how that is even done. I know how to scan for primes and perfects if I am given a large list of numbers already but not within a given range. I will show you my code to prove I do know how to solve the prime and perfect numbers if given a series of numbers rather then the range.

```import java.io.*;
import java.util.*;

public class Program3
{
public static void main(String args[] ) throws Exception
{
if (args.length < 3)
{
System.out.println("Must put filename on cmd line\n");
System.exit(0);
}
Scanner infile = new Scanner ( new File(args[0]));
while( infile.hasNext())
{
int num = infile.nextInt();
int i;
for (i = 2; i < num; i++)
{
int n = num%i;
if (n==0)
{
break;
}
}
if (i==num)
{
System.out.print(num + " ");
}
}
System.out.print("\n");

infile = new Scanner(new File(args[1]));
while(infile.hasNext())
{
int num = infile.nextInt();
int sum=0;
for(int i=1; i<num; i++)
{
if(num%i==0)
{
sum+=i;
}
if(sum==num)
{
System.out.print(num + " ");
break;
}
}
}
System.out.print("\n");

infile = new Scanner(new File(args[2]));
while( infile.hasNextLine())
{
String word = infile.nextLine();
String word1 = word.replaceAll("\\W|_", "");
String word2 = word1.toLowerCase();
int left = 0;
int right = word2.length()-1;
while(left < right)
{
if(word2.charAt(left) != word2.charAt(right))
{
break;
}
left++;
right--;
}
for (int i=0; i<left; i++)
{
if (Character.isLetterOrDigit(word.charAt(i)))
{
System.out.println(word);
break;
}
}
}
}
}

```

So basically what I was informed is that the format is primes.txt (args[0])
5 100

I would enter into my cmd prompt: java Program3 primes.txt perfects.txt palins.txt
And I would get my output. The primes and perfectt.txt's would just have 2 variables from lo to high.
My program as I have it done only read the numbers given, not the range. That is what I don't know how to do. If you run my code as now you will see it runs fine with any given series of numbers.

Any help would be appreciated. I can also give more details if you are confused as to what I need help on. I am just utterly stumped on this one part.

Is This A Good Question/Topic? 0

## Replies To: How to check for prime and perfect numbers when given a .txt file?

### #2 jon.kiparsky

• Pancakes!

Reputation: 8582
• Posts: 14,827
• Joined: 19-March 11

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:06 AM

Maybe I've misunderstood your question, but if you have a range, you can iterate over it with a for loop, no?

```int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i: list)
{
foo(i);
}

```

is precisely the same as

```for (int i=1;i <=10; i++)
{
foo(i);
}
```

### #3 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• Joined: 30-January 11

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:13 AM

Well you should first of all make two method called isPrime and isPerfect that each take an int as a parameter and return a boolean. e,g -

```private boolean isPrime(int number)
{
return true if prime, otherwise false
}
```

```int lower = 2;
int upper = 100;
```

Then you can just make a very simple for loop from lower to upper that checks each number and does whatever -

```for(int i = lower; i <= upper;i++)
{
if(isPrime(i))
// the number is prime, do something
}
```

### #4 Beastleewr11298

Reputation: 0
• Posts: 55
• Joined: 13-June 12

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:41 AM

Thank you very much for the help. But what if I don't know the range. When I am going to get my program tested I am not going to know the range of the variable so it can be 2 100 or 4 200000. But the variables will be in a .txt file from low to high. Can I assign an int to whatever number is in the file. Like infile.nextInt() or something like that. Cause I would have to have the int assigned to both incoming variables.

Ryano121, on 18 June 2012 - 11:13 AM, said:

Well you should first of all make two method called isPrime and isPerfect that each take an int as a parameter and return a boolean. e,g -

```private boolean isPrime(int number)
{
return true if prime, otherwise false
}
```

```int lower = 2;
int upper = 100;
```

Then you can just make a very simple for loop from lower to upper that checks each number and does whatever -

```for(int i = lower; i <= upper;i++)
{
if(isPrime(i))
// the number is prime, do something
}
```

I will have a range but I won't know what it is. I will be receiving a .txt file with 2 variables from low to high. But I need my program to read the incoming two numbers and scan everything in between. So just choosing 1 through 10 won't work. That is what I am confused about.

jon.kiparsky, on 18 June 2012 - 11:06 AM, said:

Maybe I've misunderstood your question, but if you have a range, you can iterate over it with a for loop, no?

```int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i: list)
{
foo(i);
}

```

is precisely the same as

```for (int i=1;i <=10; i++)
{
foo(i);
}
```

### #5 jon.kiparsky

• Pancakes!

Reputation: 8582
• Posts: 14,827
• Joined: 19-March 11

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:41 AM

Quote

Like infile.nextInt() or something like that

Have you tried it?

### #6 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• Joined: 30-January 11

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:43 AM

Yes. In your text file you have a lower and upper, there are a whole bunch of methods to retrieve the two numbers from the text file and shove them into an int.

You could use scanner.nextInt() as you suggested, or you could read the whole line as a string, split at the space and Integer.parseInt() each element etc (the first being a lot easier).

Quote

have the int assigned to both incoming variables.

I'm not sure where you are coming from there.

```int lower = scanner.nextInt();
int upper = scanner.nextInt();
```

### #7 jon.kiparsky

• Pancakes!

Reputation: 8582
• Posts: 14,827
• Joined: 19-March 11

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 11:53 AM

Ryano121, on 18 June 2012 - 01:43 PM, said:

You could use scanner.nextInt() as you suggested, or you could read the whole line as a string, split at the space and Integer.parseInt() each element etc (the first being a lot easier).

Side note: In the short run, this is true. In the long run, you might well find that you're writing input methods where you're doing significant validation, and it's easier to process the input on a per-line basis. For now, nextInt() is probably easiest, at least until you try it in scenarios where it can fail.

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11239
• Posts: 42,293
• Joined: 27-December 08

## Re: How to check for prime and perfect numbers when given a .txt file?

Posted 18 June 2012 - 12:31 PM

Rather than using an isPrime() method to help with generating primes from a to b, look at using the Sieve of Eratosthenes instead, which generates primes from 2 to b. Then in the final list, just pick out all the numbers you need starting at n >= a.