[Competition] Function Challenge #4: Java Code Golf

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

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

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

[Competition] Function Challenge #4: Java Code Golf

Post icon  Posted 31 January 2011 - 10:08 PM

Well, I have to say that I am intrigued with 's and jumptrooper's interesting challenges, so I thought I'd post it here for the Java people to try. And just to stick with the trend and to make it simple, let's do the first project found at project euler:
http://projecteuler....n=problems&id=1

Quote

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


Since this is a simple problem, let's try to find the SMALLEST piece of code that can do this task. However, I will not count spaces in the code at all because we don't want to make it too cryptic.

For scoring, I will use the Character.isWhitespace() method. If it returns false, the character count increases. Let's see how small you can get it with standard Java. (Standard libraries imports are counted, but not the contents of those classes). You may not use any non-standard Java library. Good luck.

By the way, the correct answer for this application is 233168.

Is This A Good Question/Topic? 1
  • +

Replies To: [Competition] Function Challenge #4: Java Code Golf

#2 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 31 January 2011 - 11:53 PM

class myfirstjavaprog {  
  public static void main(String args[]) {
    int sum = 0;
    for(int index = 1; index <= ((10-1)/3); index++){sum += (3 * index);}
    for(int index = 1; index <= ((10-1)/5); index++){sum += (5 * index);}
    System.out.println(sum);}}

Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

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

Posted 01 February 2011 - 03:41 AM

Alex_H, your code does not work with larger numbers than 10 because you forgot to take into account the numbers that you both multiples of 5 AND 3...you added both in there. Anyway, your code is 207 characters long. Look into using the mod operator to fix that problem.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

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

Posted 01 February 2011 - 03:48 AM

I did mine a little differently and used horrible variable names, but I sacrificed those for smaller code:
class C {
	public static void main(String[] a) {
		int s = 0;
		for (int i = 0; i < 1000; i++)
			if (i % 3 == 0 || i % 5 == 0)
				s += i;
		System.out.println(s);
	}
}



Mine scored 115.
Was This Post Helpful? 0
  • +
  • -

#5 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

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

Posted 01 February 2011 - 06:47 AM

Have you got the code you are using to test the programs? So it is easier to compare.
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

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

Posted 01 February 2011 - 08:29 AM

Modified Dogstopper's slightly :P

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



112 Characters. I'll see what I can do later on tonight.

This post has been edited by m-e-g-a-z: 01 February 2011 - 08:30 AM

Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

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

Posted 01 February 2011 - 08:50 AM

Without using a loop (as close as I could get without just printing the answer) 123 chars.

class D {
	
	public static void main(String[] a) {
		System.out.println(f(3) + f(5) - f(15));
	}
	
	static int f(int x) {
		return 999/x * (999+x - 999%x) / 2;
	}
}


A quick refactoring of Dogstopper's code: 106 chars

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



This post has been edited by cfoley: 01 February 2011 - 09:10 AM

Was This Post Helpful? 2
  • +
  • -

#8 n_yanev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-October 10

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

Posted 01 February 2011 - 12:57 PM

class D {
	public static void main(String[] args) {
		int s = 0;
		for (int i = 1; i < 1000; i++)
			s += (i % 3 == 0 | i % 5 == 0) ? i:0;

		System.out.print(s);
	}
}

This post has been edited by n_yanev: 01 February 2011 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#9 Petsarve  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 17-February 10

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

Posted 01 February 2011 - 01:59 PM

This is my try. Thought that the while loop-syntax should be shorter than a for loop. 107 characters for only printing the answer.

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


This post has been edited by Petsarve: 01 February 2011 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

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

Posted 01 February 2011 - 02:14 PM

n_yanev: Yours scored 116

Petsarve: Yours is 107

cfoley is still in the lead with 106. Let's see if we can chop off any extras! However, by making cfoley's print() instead of println(), you can get rid of two more.

@westforduk: Here is the tester program:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Main{
	
	public static void main (String[] args)
	{
		Scanner scan = null;
		try {
			 scan = new Scanner(new File("challengecode.txt"));
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "Error finding file. Make sure it" +
					"is named 'challengecode.txt' and in the same directory");
			return;
		}
		
		// Count up
		int count = 0;
		while (scan.hasNextLine()) {
			String line = scan.nextLine();
			for (int i = 0; i < line.length(); i++)
				if (!Character.isWhitespace(line.charAt(i)))
					count++;
		}
		
		JOptionPane.showMessageDialog(null, count + "");
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 Petsarve  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 17-February 10

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

Posted 01 February 2011 - 02:27 PM

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



This is 105, though it would be hard for a human to read the output. It find the number, though, and that's the challange, right? :smartass:

EDIT: Oh, nevermind. Wont compile ;)

This post has been edited by Petsarve: 01 February 2011 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

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

Posted 01 February 2011 - 06:02 PM

Was playing with this. I thought I found a loophole but it still came in at 141 chars though. If only there was a whitespace interpretor in the Java API!

class C {
	public static void main(String[] a) {
		String s="",i = s;
			for (
					; 
					!i.equals("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ");
					i+= " ") 
				s += i.matches("(   )*|(     )*") ? i:"";
		System.out.println(s.length());
	}
}

Was This Post Helpful? 0
  • +
  • -

#13 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 - 12:10 AM

I managed to cut off 4 more characters although the code is more difficult to understand now

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

Was This Post Helpful? 2
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

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

Posted 02 February 2011 - 01:35 AM

Give that man a cookie!

You'll kick yourself though because this gives the same result:

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


Was This Post Helpful? 1
  • +
  • -

#15 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 - 01:48 AM

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

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