it's supposed to find the largest prime factor of a:

long a = 55;

long max = 0;

int m = 0;

for(long i = 3; i < a; i++){

for(long k = 2; k < i; k++){

if(i%k == 0){

m = 1;

}

}

if(m==0){

if(a%i==0){

if(i > max){

max = i;

}

}

}

}

Printable View

- 08-07-2012, 01:56 AMabvWhy doesn't this code work?
it's supposed to find the largest prime factor of a:

long a = 55;

long max = 0;

int m = 0;

for(long i = 3; i < a; i++){

for(long k = 2; k < i; k++){

if(i%k == 0){

m = 1;

}

}

if(m==0){

if(a%i==0){

if(i > max){

max = i;

}

}

}

} - 08-07-2012, 02:37 AMawinstonRe: Why doesn't this code work?
Perhaps a better question: why

*should*it work? - 08-07-2012, 03:31 AMJunkyRe: Why doesn't this code work?
When posting code wrap it in code tags to preserve indenting.

What happens if m == 1?

If m does equal 1 should it be reset for the next loop iteration?

If you objective is to find the LARGEST prime factor then why do you start at the SMALLEST? - 08-07-2012, 12:32 PMDarrylBurkeRe: Why doesn't this code work?
Forum Rules -- especially the third paragraph

http://www.java-forums.org/forum-gui...w-members.html

BB Code List - Java Programming Forum

db