You should be right, but when I tried it I got the same answer. I wonder if it's a bug with the JVM I'm using. I did search the bug reports but no luck.

## 35 Replies - 17916 Views - Last Post: 25 July 2013 - 03:34 AM

### #16

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 03:54 AM

### #17

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 03:56 AM

I tested the code and also got the correct answer....very strange indeed lol

### #18

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 05:59 AM

The goal is smallest code possible now? What an odd aspiration for Java.

Still, silly is fun. For the smallest code, I offer this:

Still, silly is fun. For the smallest code, I offer this:

int f(int n) { return (--n<1)?0:f(n)+(n%3*n%5>0?0:n); }

### #19

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 07:33 AM

... but that's not a Java program baavgai

I tried stuff like that (also from recursive constructors!) but calling methods from static main was too many characters.

I tried stuff like that (also from recursive constructors!) but calling methods from static main was too many characters.

### #20

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 08:54 AM

93 characters... but you need assertions enabled.

class C { public static void main(String[] a) { for (int s=0,i=0;;)/> { s += ++i%3*i%5 > 0 ? 0:i; assert i < 999 : s; } } }

### #21

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 12:08 PM

cfoley, on 02 February 2011 - 09:33 AM, said:

... but that's not a Java program baavgai

Agreed, it is clearly a Java function, which is far more useful...

However, I have yet to see some of your programs offer output, which is also fundamentally useless as a program.

Using the logic offered, this is a valid "program." It's 104 chars:

class C { static int f(int n) { return (--n<1)?0:f(n)+(n%3*n%5>0?0:n); } public static void main(String[] a) { f(1000); } }

A more honest offering would be this, at 124 chars:

class C { static int f(int n) { return (--n<1)?0:f(n)+(n%3*n%5>0?0:n); } public static void main(String[] a) { System.out.println(f(1000)); } }

The declaration in some solutions of the sum in the loop means your really can't print it...

Now that I more fully understand the house rules... 86 chars.

class C { public static void main(String[] a) { int s=0,n=1000; while(--n>0) { s += n%3*n%5>0?0:n; } } }

Note, I used a very *nixy method of counting chars:

cat C.java | tr '\n' ' ' | sed 's/\s//g' | wc -c

### #22

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 02:27 PM

You social hacker you. Read between the line you did, and you managed to pull of a sly victory.

### #23

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 06:18 PM

Refactored baagvai's code....85 characters

class C { public static void main(String[] a) { int s=0,n=999; while(n-->0) { s += n%3*n%5>0?0:n; } } }

### #24

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 02 February 2011 - 06:34 PM

I did print my answer every time. Sometimes it appeared somewhere in the err output...but it was there

Here's one that calculates it but does not print it (82 chars)

and if you want the declaration outside the loop it's 83 chars.

Here's one that calculates it but does not print it (82 chars)

class C { public static void main(String[] a) { for (int s=0,i=0;i < 999;) s += ++i%3*i%5 > 0 ? 0:i; } }

and if you want the declaration outside the loop it's 83 chars.

class C { public static void main(String[] a) { int s=0,i=0; for (;i < 999;) s += ++i%3*i%5 > 0 ? 0:i; } }

### #25

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 08 June 2011 - 03:04 AM

class Competition { public static void main(String[] args) { int sum=0; for (int i=3; i<1000; i++) { if(i %3 == 0 || i%5 == 0) { sum += i; } } System.out.println("Sum : " + sum); } }

Thanks

itengineer

### #26

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 03 July 2011 - 05:15 PM

A totally different approach.

First some math:

Lets look at the numbers we are trying to add for lets say 3.

n3 = 3, 6, 9, 12, 3n <- this is called mathematically a a sequence with general term 3n

Now we need the running sum of this seqience. The sums of the terms of a sequence is called a series.

s3 = 3, 9, 18, 30, sum from i = 0 to m of 3*i; <- This is the series of sequence n3.

Now for a lot of basic series, calculating that summation is not the only way to find the m-th term.

for this one:

sum from i = 0 to m of 3*i = (m+1)*m*3/2.

But we don't know m at this point or do we ? Well m is the number of elements of the row n3 < 1000, yes or in other workds floor(999/3).

We can do the same for 5.

But we've made a mistake, we've actually counted everything that can be divided by 3 and 5 twice. But no worry we can do the same as we did above for those numbers but this time for 5*3 and subtract that.

Using this we have a closed formula for our problem.

Now how does this translate in a program, something like this:

I couldn't get it short enough though.

P.S.: Is there any way to write math in this forums, any latex support by any chance ?

First some math:

Lets look at the numbers we are trying to add for lets say 3.

n3 = 3, 6, 9, 12, 3n <- this is called mathematically a a sequence with general term 3n

Now we need the running sum of this seqience. The sums of the terms of a sequence is called a series.

s3 = 3, 9, 18, 30, sum from i = 0 to m of 3*i; <- This is the series of sequence n3.

Now for a lot of basic series, calculating that summation is not the only way to find the m-th term.

for this one:

sum from i = 0 to m of 3*i = (m+1)*m*3/2.

But we don't know m at this point or do we ? Well m is the number of elements of the row n3 < 1000, yes or in other workds floor(999/3).

We can do the same for 5.

But we've made a mistake, we've actually counted everything that can be divided by 3 and 5 twice. But no worry we can do the same as we did above for those numbers but this time for 5*3 and subtract that.

Using this we have a closed formula for our problem.

Now how does this translate in a program, something like this:

class C { public static void main(String[] a) { System.out.println(f(3)+f(5)-f(3*5)); } static int f(int i) { int m = 999/i; return (m+1)*m*i/2; } }

I couldn't get it short enough though.

P.S.: Is there any way to write math in this forums, any latex support by any chance ?

This post has been edited by **Karel-Lodewijk**: 03 July 2011 - 05:19 PM

### #27

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 03 July 2011 - 06:31 PM

### #28

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 04 July 2011 - 02:53 AM

Karel-Lodewijk, I tried that method a few posts back but ended up sticking with variants of the loop method because they used less characters.

### #29

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 01 September 2012 - 10:56 AM

This works

class Sum { public static void main(String[] a) { int sum = 0; for (int i = 0; i < 1000; i++) sum += i % 3 == 0 || i % 5 == 0 ? i : 0; System.out.println(sum); } }

### #30

## Re: [Competition] Function Challenge #4: Java Code Golf

Posted 07 September 2012 - 03:53 PM

I am not that advance at maths, so a problem like this is difficult for me. Therefore, I do not know what formulas you are using. But if you havnt seen it, there is a listing of this problem on wikipedia. It doesnt show you the answer, it does tell you the most effecient formula though. Maybe it can be used to come up with an even shorter answer?

http://en.wikipedia....m_and_solutions

http://en.wikipedia....m_and_solutions