[Competition] Function Challenge #4: Java Code Golf

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

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

#16 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

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

Posted 02 February 2011 - 03:54 AM

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.
Was This Post Helpful? 0
  • +
  • -

#17 Alex_H  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 27-January 11

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
Was This Post Helpful? 0
  • +
  • -

#18 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

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. :P

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); }


Was This Post Helpful? 1
  • +
  • -

#19 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

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.
Was This Post Helpful? 0
  • +
  • -

#20 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

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;
		}
	}

}


Was This Post Helpful? 0
  • +
  • -

#21 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

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

Posted 02 February 2011 - 12:08 PM

View Postcfoley, 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... :P

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


Was This Post Helpful? 1
  • +
  • -

#22 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#23 Alex_H  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 27-January 11

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

Posted 02 February 2011 - 06:18 PM

Refactored baagvai's code....85 characters :bigsmile:

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

Was This Post Helpful? 0
  • +
  • -

#24 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

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 :D

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;
	}
}

Was This Post Helpful? 0
  • +
  • -

#25 itengineer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-April 09

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
Was This Post Helpful? 0
  • +
  • -

#26 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

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:

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

Was This Post Helpful? 0
  • +
  • -

#27 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

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

Posted 03 July 2011 - 06:31 PM

Latex support has be requested before, skyhawk133 and his team are looking at it.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#28 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

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.
Was This Post Helpful? 0
  • +
  • -

#29 peterrankings  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-September 12

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

Posted 01 September 2012 - 10:56 AM

This works :bananaman:
 
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);
	}
}


Was This Post Helpful? 0
  • +
  • -

#30 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

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
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3