harmonic divisors

this code is supposed to print the numbers 1, 6, 28, 140, 270, 496, 672 but its not printing anything.

Does anyone know what i've done wrong?

class HarmonicDivisorNumbers{

publicstaticvoid main(String[] args){

for(int i = 1; i<1000; i++)

if(isHarmonicDivisor(i)) System.out.print(i+" ");

}

staticboolean isHarmonicDivisor(int n){

int sum = 0, divisorsOfN = 0;

for(int divisor = 1; divisor <=n; divisor++){

if(n % divisor == 0){

sum = sum +(1/divisor);

divisorsOfN++;

}

}

if( (divisorsOfN/sum) == 0)returntrue;

elsereturnfalse;

}

}

[1610 byte] By [mark_8206a] at [2007-11-27 10:23:58]
# 1

First, your code won't compile as you are missing braces.

public class HarmonicDivisorNumbers

{

public static void main(String[] args)

{

for(int i = 1; i<1000; i++) //you missed a brace

{

if(isHarmonicDivisor(i)) System.out.print(i+" ");

}

}

public static boolean isHarmonicDivisor(int n)

{

int sum = 0, divisorsOfN = 0;

for(int divisor = 1; divisor <=n; divisor++)

{

if(n % divisor == 0)

{

sum = sum +(1/divisor);

divisorsOfN++;

}

}

//if( (divisorsOfN/sum) == 0) return true;

//else return false;

//can be rewritten as

return (divisorsOfN/sum) == 0;

} //missed this brace

}

Second, a good way to debug is to place print statements at various spots and watch the values of your variables as the math is performed.

Try that and see what you get.

Good luck.

filestreama at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 2

> this code is supposed to print the numbers 1, 6, 28,

> 140, 270, 496, 672 but its not printing anything.

> Does anyone know what i've done wrong?

>

> > class HarmonicDivisorNumbers {

>

> public static void main(String[] args) {

>

> for(int i = 1; i<1000; i++)

> if(isHarmonicDivisor(i)) System.out.print(i+" ");

>

> }

>

> static boolean isHarmonicDivisor(int n) {

>

> int sum = 0, divisorsOfN = 0;

>

> for(int divisor = 1; divisor <=n; divisor++) {

>

> if(n % divisor == 0) {

> sum = sum +(1/divisor);

> divisorsOfN++;

> }

>

> }

>

> if( (divisorsOfN/sum) == 0) return true;

> else return false;

>

> }

>

> }

>

if(isHarmonicDivisor(i)) System.out.print(i+" ");

}

change into:

if(isHarmonicDivisor(i)){

System.out.print(i+" ");

}

deAppela at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 3

aww filestream beat me to it ;(

deAppela at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 4

> this code is supposed to print the numbers 1, 6, 28,

> 140, 270, 496, 672 but its not printing anything.

> Does anyone know what i've done wrong?

>

> ...

> if( (divisorsOfN/sum) == 0) return true;

> else return false;

...

>

divisorsOfN/sum can only be zero if divisorsOfN is less than sum. Which I think will never be the case here.

Could it be that you're suffering from integer division truncation?

You do know thatSystem.out.println(1/2);

will print 0 right?

Or is it on purpose?

dwga at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 5

> this code is supposed to print the numbers 1, 6, 28,

> 140, 270, 496, 672 but its not printing anything.

> Does anyone know what i've done wrong?

>

> ...

Aaaw, comon Mark, I have suggested in at least 5 threads of yours how to debug code by inserting some System.out.println's to see what's happening with your variables and why "things" are not working as you suspect them to be (like filestream suggested already).

prometheuzza at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 6

technically

public static void main(String[] args) {

for(int i = 0; i < 1000; i++)

if(isHarmonicDivisor(i))

System.out.print(i+" ");

}

is just fine... don't need a brace.

jGardnera at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 7

OK i'll go put in some system.out.println's and see if i can work it out!

mark_8206a at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 8

> OK i'll go put in some system.out.println's and see

> if i can work it out!

; )

Thank you for your consideration!

prometheuzza at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 9

You are right in that you don't need a brace but having been bit by the

if (expression)

do1;

and later requiring do2..doN associated with the conditional, I tend to put braces everywhere. ;-)

filestreama at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 10

if( (divisorsOfN/sum) == 0) return true;

else return false;

i know that what's above is wrong, i need it to be:

if( (divisorsOfN / sum) == an integer) return true;

else return false;

can anyone show me how that's done?

mark_8206a at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 11

> if( (divisorsOfN/sum) == 0) return true;

> else return false;

>

> i know that what's above is wrong, i need it to be:

> if( (divisorsOfN / sum) == an integer) return true;

> else return false;

>

> can anyone show me how that's done?

Run these lines:

System.out.println(2.1 % 1);

System.out.println(2.00000001 % 1);

System.out.println(2.0 % 1);

Good luck.

prometheuzza at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 12

i changed it to

if( (divisorsOfN % sum) == 1) return true;

else return false;

is that correct?

i think there's also something wrong with the sum variable.

mark_8206a at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 13

> i changed it to

> > if( (divisorsOfN % sum) == 1) return true;

> else return false;

>

>

> is that correct?

>

> i think there's also something wrong with the sum

> variable.

((divisorsOfN / sum) %1) == 0

But perhaps you need to build in a small error margin because of all the floating point divisions going on. Something like this:((divisorsOfN / sum) %1) < 0.0000000001

prometheuzza at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 14

Don't even deal with doubles. This can be calculated exactly. I would work only with integer (or long) numerators and denominators and not do the division. Then if numberator % denominator == 0 at the end, you've got an integer.

petes1234a at 2007-7-28 17:24:57 > top of Java-index,Java Essentials,New To Java...
# 15

thanks prome

if that's been corrected then there's still something wrong, i think it's the line sum = sum +(1/divisor);

mark_8206a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 16

see my previous reply

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 17

Let me show you what I mean with a concrete example. Take the number 6. The divisors are 1, 2, 3, and 6.

sum = 1/1 + 1/2 + 1/3 + 1/6

sum = 6/6 + 3/6 + 2/6 + 1/6

numerator = 6 + 3 + 2 + 1 = 12

denominator = 6

numerator % denominator => 12 % 6 == 0

Now generalize this for all numbers (you may want to calc a least common divisor to prevent the numbers from getting too large) and you have the exact solution.

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 18

error -- deleted note

Message was edited by:

petes1234

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 19

petes, i understood your posts and i tried to use it in my code, the code is now almost correct, but the numbers 140 and 270 are missing in the output, do you know what i've missed?

class HarmonicDivisorNumbers {

public static void main(String[] args) {

for(int i = 1; i<1000; i++)

if(isHarmonicDivisor(i)) System.out.print(i+" ");

}

static boolean isHarmonicDivisor(int n) {

int sum = 0, divisorsOfN = 0;

for(int divisor = 1; divisor <=n; divisor++) {

if(n % divisor == 0) {

sum=sum+divisor;

}

}

if(sum % n == 0) return true;

else return false;

}

}

mark_8206a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 20

I've got my calculations to work, but I had to use BigIntegers. Also my previous final equation was wrong as I didn't have the count of divisors in there. This has been corrected. It's all doable but again with I had to use BigInts.

I also broke things down more. I had a method called isHarmonicDivisor(n) that in pseudocode looked like this:

boolean method isHarmonicDivisor(int n)

calc divisors of n and place in List

use List to calc bigInt numerator and denominator

BigInteger superNumerator = denom * size of List // must use big int multiplication!

BigInteger mod result = superNumerator mod numerator // must use big int mod

if mod result equals BigInteger.ZERO return true

else return false

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 21

I don't like that part

sum = sum +(1/divisor);

S_i_m_ua at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 22

> I don't like that part

> sum = sum +(1/divisor);

nor do I. It makes the program deal with floating point arithmetic. This program requires a precise answer.

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 23

> I've got my calculations to work, but I had to use

> BigIntegers. Also my previous final equation was

> wrong as I didn't have the count of divisors in

> there. This has been corrected. It's all doable but

> again with I had to use BigInts.

>

> I also broke things down more. I had a method called

> isHarmonicDivisor(n) that in pseudocode looked like

> this:

>

> [code]

>boolean method isHarmonicDivisor(int n)

>calc divisors of n and place in List

> use List to calc bigInt numerator and

> denominator

> BigInteger superNumerator = denom * size of

> List // must use big int multiplication!

> BigInteger mod result = superNumerator mod

> numerator // must use big int mod

> if mod result equals BigInteger.ZERO return

> true

> else return false

petes, i'm not familiar with BigInts, could this be done any other way?

or could you give me any links so i could read about them?

mark_8206a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 24

> petes, i'm not familiar with BigInts, could this be

> done any other way?

> or could you give me any links so i could read about

> them?

No problem. When I need to look an API like this up, I just google:

java platform biginteger

That will point you right here:

http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 25

> No problem. When I need to look an API like this up,

> I just google:

>

> java platform biginteger

>

> That will point you right here:

>

> http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

Why not set a bookmark:

http://java.sun.com/javase/6/docs/api/

BigDaddyLoveHandlesa at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 26

> Why not set a bookmark:

>

> http://java.sun.com/javase/6/docs/api/

Because of course that would be much too easy and logical. I need to do things the hard way. "D'Oh!" Truthfully though, I don't particularily like the way that the initial api page is set up. I know, I'm weird.

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 27

No need for those BigInts!

; )

Let denominator be the sum of the divisors of N and let numerator be the number of divisors of N multiplied by N itself. Now N is harmonic if numerator | denominator.

Here's a short algorithm:

static boolean isHarmonicDivisor(final int N) {

long num = 0L;

long den = 0L;

for(int i = 1; i <= N; i++) {

if(N%i == 0) {

den += i;

num++;

}

}

return (num*N)%den == 0;

}

prometheuzza at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 28

> No need for those BigInts!

> ; )

wow... that looks too easy! Let me check this out...

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 29

well shut my mouth and I'll be dmned.... You are absolutely right! I am unworthy of being in your presence.

petes1234a at 2007-7-28 17:25:02 > top of Java-index,Java Essentials,New To Java...
# 30

> well shut my mouth and I'll be dmned.... You are absolutely right!

Did you doubt me?

; )

> I am unworthy of being in your presence.

Ooow, cut it out!

prometheuzza at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 31

prome muchas gracias,

eres un genio!

mark_8206a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 32

> prome muchas gracias,

> eres un genio!

You're welcome, but don't just hand that in as your own, OK?

prometheuzza at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 33

> > prome muchas gracias,

> > eres un genio!

>

> You're welcome, but don't just hand that in as your

> own, OK?

it's not homework! i just wanted to know if i could do it .....and i couldn't!

mark_8206a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 34

mark_8206:

> if that's been corrected then there's still something wrong, i think it's the line

> sum = sum +(1/divisor);

I was wondering whether you had seen this by dwg when you made this statement:

> You do know that System.out.println(1/2) will print 0 right?

And whether you figured out why it would print 0 (as this knowledge will come in handy the next time you try to do a number puzzle like this).

EvilBroa at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 35

> mark_8206:

> > if that's been corrected then there's still

> something wrong, i think it's the line

> > sum = sum +(1/divisor);

>

> I was wondering whether you had seen this by dwg when

> you made this statement:

> > You do know that System.out.println(1/2) will print

> 0 right?

>

> And whether you figured out why it would print 0 (as

> this knowledge will come in handy the next time you

> try to do a number puzzle like this).

i'm not sure what was wrong with the line "sum = sum +(1/divisor);" but it seems it was not accurate enough to print the correct numbers, as for "System.out.println(1/2) will print 0", is it because i had not declared the answer of 1/2 as a float?

mark_8206a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 36

> i'm not sure what was wrong with the line "sum = sum +(1/divisor);"

Dividing an int by another int will produce an int, so 1 divided by any int > 1 will produce 0.

EvilBroa at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 37

> prome muchas gracias,

> eres un genio!

Mark, eres t hispanoparlante?

petes1234a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 38

> > prome muchas gracias,

> > eres un genio!

>

> Mark, eres t hispanoparlante?

petes estoy aprendiendo hablar espanol, lo hago desde hace dos anos!

mark_8206a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 39

entonces buenas suerte

petes1234a at 2007-7-28 17:25:07 > top of Java-index,Java Essentials,New To Java...