Perl Challenge: Code Golf!

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

33 Replies - 13977 Views - Last Post: 08 October 2012 - 01:47 AM

#31 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Perl Challenge: Code Golf!

Posted 18 February 2011 - 09:38 AM

Awesome job evolivid! Very impressive! +50 BONUS POINTS and +3 ALTERNATIVE POINTS for bringing the command line into it!
I think for this challenge - I'm going to accept the 38c entry as shortest, since I didn't say one way or the other whether it had to be a script file or command line (right? I don't remember now...) But you definitely have given me a new idea for some challenges down the road!! THANKS!
Was This Post Helpful? 0
  • +
  • -

#32 evolivid  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 24-July 08

Re: Perl Challenge: Code Golf!

Posted 19 February 2011 - 04:26 PM

cool.. thanks for the challenge, im looking at some of my old code in a new way now!
on to round 2!
Was This Post Helpful? 0
  • +
  • -

#33 Abcxyzp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-September 12

Re: Perl Challenge: Code Golf!

Posted 25 September 2012 - 07:48 PM

A late post, could be 2 characters less if using -E + 'say'.

# 36 non-white space characters (38 including white spaces):

Spoiler


It's amazing that even though perl has to add so many '$' sign before the variables, it is still the shortest for one liner.
Was This Post Helpful? 0
  • +
  • -

#34 mendeepak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-October 12

Re: Perl Challenge: Code Golf!

Posted 08 October 2012 - 01:47 AM

View Postjumptrooper, on 27 January 2011 - 10:25 AM, said:

Perl writers love their compact statements and one-liners, so....

Let's play a game!

It's called "Code Golf". I didn't come up with the idea - I've seen it on the web before. It's very simple:

See who can write the shortest amount of code to accomplish a given task.

That's it. Speed is inconsequential! Time is not of the essence! Just make it short! The code with the fewest number of non-whitespace characters wins! Don't worry about line numbers - just character count.

The problem is problem #1 from ProjectEuler.net**, which reads:

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.


Your code should print the result to the screen.

This friendly game is open to anyone, using any language - But we are especially interested in your contribution if you are a Perl programmer!


*Oh - and if you are not a Perl programmer, you get BONUS POINTS if you try it in Perl!!!*


**ProjectEuler.net is a great site! There are literally hundreds of unique and interesting computational problems to work through and there's a big community around finding optimal solutions for each of them.


my $sum;
foreach my $val(1..999){
(($val % 3) == 0 || ($val % 5) ==0) ? $sum+=$val : next ;
}
print $sum;

Attached File(s)

  • Attached File  1.txt (115bytes)
    Number of downloads: 31

Was This Post Helpful? 0
  • +
  • -

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