[Competition] Function Challenge #4: Java Code Golf

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

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

#31 carlking1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 12

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

Posted 09 September 2012 - 06:19 AM

View PostAlex_H, on 02 February 2011 - 06:18 PM, said:

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

I had the same idea as you but the total sum is 232169 instead of 233168. When n goes into the while loop, it will be 998 instead of 999.
Was This Post Helpful? 0
  • +
  • -

#32 Takwas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 05-September 12

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

Posted 12 November 2012 - 04:44 PM


class N {
	   
	public static void main(String[] a){
		int i=0, s = 0;
      
		while (i++ <999)
			s += i%3==0|i%5==0 ? i : 0;
		System.out.print(s);
	}
}



Was This Post Helpful? 0
  • +
  • -

#33 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

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

Posted 01 December 2012 - 08:26 AM

Made some modifications to the previous code:

class N{
   static void a(){
      int i=0,s=0;while(i++ <999)s+=i%3==0|i%5==0?i:0;
      System.out.print(s);}}


I removed the public identifier in the method as well as the String args[].I also changed the method name to a. Its 89 characters if whitespace isn't included.

Here's my code that I made on my own in case the previous doesn't count:

class A
{
    static void a()
    {
        int x=0,i=0;
        for (;i<=999;i++)
            if (i%3==0|i%5==0)
                x+=i;
        System.out.print(x);
    }
}



Again, there is no public identifier, and no String args[], as well as a change in the name of the method. 93 characters with the whitespace substracted.

If you don't need to print the answer, here are versions of both that do the trick:

class N{
   static void a(){
      int i=0,s=0;while(i++ <999)s+=i%3==0|i%5==0?i:0;}}


class A
{
    static void a()
    {
        int x=0,i=0;
        for (;i<=999;i++)
            if (i%3==0|i%5==0)
                x+=i;
    }
}



It gets rid of twenty characters for each, bringing the totals to 69 and 73 characters respectively if the white space doesn't count.
Was This Post Helpful? 0
  • +
  • -

#34 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,424
  • Joined: 29-July 11

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

Posted 04 December 2012 - 08:32 AM

View PostAlex_H, on 02 February 2011 - 08:48 AM, said:

See I didn't think that would work because the * and % operator have the same precedence so the resulting equation would be

( (i%3) * i ) % 5)

or am I missing something here?


++i%3*(i%5)

(++i) % 3 * i % 5;

I believe they're all the same.

This post has been edited by farrell2k: 04 December 2012 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#35 Kemical  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 27-November 12

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

Posted 05 December 2012 - 11:25 PM

Just an attempt by a novice java guy,

Is this a good way to get a character count without spaces?

chris@slack:~/d/5-3$ tr -d [:space:] < T.java | wc -c
153

Although I see that a test % of each integer as you increment is shorter. Oh well, I think its a good try for a newbie :)/>

Let met know what you think..


public class T
{
public static void main(String[] a) 
{
	int sum=0, i=3,x=5;
	for( ; i<1000;i+=3,x+=5)
	{	
		sum += i;
		if(i%5==0)
			sum -=i;
		if(x<1000)
			sum+=x;
	}
	System.out.println(sum);
}
}



Was This Post Helpful? 0
  • +
  • -

#36 Binisha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-July 13

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

Posted 25 July 2013 - 03:34 AM

public class S{
		public static void main() {
			int s = 0;
			for (int i = 0; i < 1000; i++) {
				if (i % 3 == 0 || i % 5 == 0) {
					s += i;
				}}System.out.print(s);	}}

Was This Post Helpful? 0
  • +
  • -

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