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]

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.
> 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+" ");
}
aww filestream beat me to it ;(
> 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 >

> 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).
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.
OK i'll go put in some system.out.println's and see if i can work it out!
> OK i'll go put in some system.out.println's and see
> if i can work it out!
; )
Thank you for your consideration!
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. ;-)
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?
> 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.
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.
> 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
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.
thanks prome
if that's been corrected then there's still something wrong, i think it's the line sum = sum +(1/divisor);
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.
error -- deleted note
Message was edited by:
petes1234
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;
}
}
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
I don't like that part
sum = sum +(1/divisor);
> 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.
> 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?
> 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
> 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/
> 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.
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;
}
> No need for those BigInts!
> ; )
wow... that looks too easy! Let me check this out...
well shut my mouth and I'll be dmned.... You are absolutely right! I am unworthy of being in your presence.
> 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!
prome muchas gracias,
eres un genio!
> prome muchas gracias,
> eres un genio!
You're welcome, but don't just hand that in as your own, OK?
> > 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_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).
> 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?
> 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.
> prome muchas gracias,
> eres un genio!
Mark, eres t hispanoparlante?
> > prome muchas gracias,
> > eres un genio!
>
> Mark, eres t hispanoparlante?
petes estoy aprendiendo hablar espanol, lo hago desde hace dos anos!