So open challenge, code golf this, standard code golfing rules apply. PM me your code, several versions are OK, challenge closes at 7 Jan 7:00AM GMT. I'll keep a leaderboard here and update it continuously as submissions come in and release the code after the challenge is done.
I have my single submission, I, of course, cannot improve upon it when I begin receiving submissions. So my submission is immutable.
For brevity and execution speed, this challenge is for only numbers up to 1000. You may use any language and they will be placed in the following categories (partially arbitraily chosen):
a) perl
b) php
c) python
d) ruby
e) anything and everything else
expected output:
Notice, space delimited. Trailing whitespace will be removed from your submission before it is compared against the expected output.
EDIT: A few of you have been not getting the formatting down so, the md5sum of solution is: e5520566aa3e1456e05d1f17d91ab9d4
To check your solution simply run the program, strip the whitespace from the end and pipe the output to an md5sum utility. (contact me if you need help doing this.)
EDIT: Clarification, output is expected to STDOUT, not to a file.
EDIT: Code written in a different language will be accepted and placed into another category, I just don't know how much interest this will generate so I didn't want to overwhelm people.
EDIT: Yes, you may submit code as many times as you would like in as many different languages as you like.
EDIT: Clarification, leaderboard values are in characters, not in lines.
EDIT: Clarification, of the categories and allowed languages.
EDIT: Removed the no trailing whitespace requirement, walking through the submissions to find the ones that I rejected under that rule but are valid now.
* (immutable score)
Leaderboard update: updated at 12/30/2008 17:30 GMT, several new versions were submitted, I don't have time to test them all yet, remember your md5sum should be e5520566aa3e1456e05d1f17d91ab9d4
Leaderboard update: updated at 12/31/2008 3:15 GMT, removed rejected versions, will send PM replies instead, flagitious blew right into the lead with an amazing ruby hack, I still haven't tested the PHP code, I'll get that done in the next few hours.
Leaderboard update: updated at 1/1/2009 2:15 GMT, Blublu updated his java version and shaved a few characters, pauli updated his ruby submission a bit and shaved of a few as well.
Leaderboard update: updated at 1/1/2009 6:00 GMT, pauli submitted a C# and a php version, added them
Leaderboard update: updated at 1/2/2009 19:30 GMT, should be up to date, if I'm missing you let me know.
Leaderboard update: Pauli and blublu are duking it out java vs. C#, it's kinda entertaining, *popcorn*
Leaderboard update: updated at 1/7/2009 20:00 GMT, all submissions have been updated, I'm writing up the synopsis of the code received, thanks for the submissions. It's been fun. :D
Leaderboard update: updated at 1/7/2009 20:30 GMT, opps missed pauli's 75-byte PHP solution, corrected
Build a man a fire, warm him for a day,
Set a man on fire, warm him for the rest of his life.
Joined: 11/30/2008
Posts: 650
Location: a little city in the middle of nowhere
Just learned something: python is incredibly easy to learn. Sending you code in about 10 mins.
EDIT: I have a question: can the output be 1.0, 2.0, 3.0, 4.0 ect. I made my own "prime" function and it turns out I can use less characters if it prints to one decimal place.]
I think GCC is pretty standard compliant, isn't it?
<OmnipotentEntity> ShinyDoofy, if you write a submission in C or pascal I'll accept it.
<OmnipotentEntity> provided I can get it to compile and run.
gcc supports compiling standard-compliant C. It also supports compiling C from 20 years ago, which looks a bit different. For example, gcc will compile this C "program" just fine:
main(){puts("Hello");}
Mind you, that's the *entire* program. And it certainly is not standard-compliant (for instance, it lacks the proper #include, the return type of main() as well as its parameter type).
I'll throw in this C++ entry. It sports an example of metaprogramming. In telling the compiler what kind of program we're designing, we're making the compiler carry out the necessary calculations. The resulting program simply prints the results and does no calculations whatsoever. Mind you, this is not very typical C++ programming style.
Note that some compilers may have trouble compiling this entry. For instance, it requires a template recursion depth of some 500. (Another topic is Microsoft's C++ compiler (CL version 12.00.8168), which fails to even understand the syntax.)
#include <iostream>
template<int p, int m> struct prime { enum {
// Handling two iterations at once to overcome compiler limitation
// with maximum template recursion depth
result = ((p%m) != 0) && ((p%(m-1)) != 0) && prime<p,m-2>::result
}; };
template<int p> struct prime<p,1> { enum { result = 1 }; };
template<int p> struct prime<p,0> { enum { result = 1 }; };
template<int guess, int value, int i> struct int_sqrt_floor {
enum { result = int_sqrt_floor<guess-(guess*guess-value)/(2*guess), value, i+1>::result
}; };
template<int guess, int value> struct int_sqrt_floor<guess,value,7> { enum { result = guess }; };
template<int value, int i> struct int_sqrt_floor<0,value,i> { enum { result = 0 }; };
template<int o> struct sieve { enum {
result = (o==1) || (o%7 && o!=3
&& !prime<o,int_sqrt_floor<o/2,o,1>::result+1>::result)
}; };
template<int k> struct sieveprint { sieveprint(std::ostream& o) {
(sieveprint<k-5>(o));
// loop unrolling to overcome compiler limitation
// of maximum template recursion depth
if(sieve<k-4>::result) o << k-4 << ' ';
if(sieve<k-3>::result) o << k-3 << ' ';
if(sieve<k-2>::result) o << k-2 << ' ';
if(sieve<k-1>::result) o << k-1 << ' ';
if(sieve<k>::result) o << k << ' ';
} };
template<> struct sieveprint<0> { sieveprint(std::ostream&) {} };
int main() { (sieveprint<1000>(std::cout)); }
Obviously, the goal of this entry is not to get the smallest filesize. I just posted it for fun.
Joined: 1/16/2008
Posts: 358
Location: The Netherlands
I like such mini competitions (although I can imagine some ppl will probably put quite some time into it :D)
thought I'd write a haskell version of it... I suppose it can be shortened but at the moment I don't have the time to look into it
ps. it builds the entire (infinite) list
ps2. it's not efficient at all
1:[x|x<-[2..],mod x 7>0,[y|y<-[1..x],mod x y<1]!!1/=x]
edit4: fixed to display with the help of bisqwit
tried to make it display the list using
map (putStr.show) myList
but it doesn't work
I tried Haskell first, but I couldn't figure out how to do AND conditions. Also, your code fails to print the result. EDIT: Rest of the post not applicable anymore.
Joined: 1/16/2008
Posts: 358
Location: The Netherlands
Bisqwit wrote:
EDIT: It fails to compile as well.
$ ghc tmp.hs
tmp.hs:2:0: parse error (possibly incorrect indentation)
EDIT 2: Fails to honor the mission goals too. It should exclude numbers divisible by 7.
yeah sorry, the code wouldn't be displayed properly so part of it got 'removed'
you're right about the result not being displayed though... I don't know how to do it in haskell :shame:
yeah sorry, the code wouldn't be displayed properly so part of it got 'removed'
I sent you a private message, but since you aren't reading it... might as well say it here for the benefit of others as well.
Do this:
[V] Disable HTML in this post
This lets phpBB understand that when you are posting < and >, you don't mean a HTML tag, and it won't try to sanitize them.
While you are at it, you might go to your profile page and set the HTML setting disabled by default, since it's very rare that you actually need it and since the inconvenience with posts containing < and > mingled is usually greater than is the inconvenience of having to uncheck the box when you do want to use HTML markup.
Maple should be a breeze, something like
print "1";
for (n=4..1000)
if (!isprime(n) and n%7!=0) then print " "+n;
fi
rof
Well that or anything else with primality checking in its standard library (Mathematica, Matlab, MathCad)...
I don't know enough Perl to stand a chance, but I do know some Python. Here's 118 chars:
import sys
w=sys.stdout.write
w('1')
for i in range(1001):
if i%7!=0 and any([i%n==0 for n in range(2,i)]):w(' %d'%i)
Currently working on:
SNES Star Fox, Level 3 (100%, published)
SNES Star Fox, Level 2 (33%, after Sector X)
SNES Star Fox 2, Expert mode (100%, published)
Oh believe me, I'd be happy if people improved on my code. :D
Currently working on:
SNES Star Fox, Level 3 (100%, published)
SNES Star Fox, Level 2 (33%, after Sector X)
SNES Star Fox 2, Expert mode (100%, published)
The best PHP I can come up with in an hour or so is <strike>92</strike> 91 bytes, <strike>89</strike> 88 if you allow <? ?> :p
I got 134, under the condition that it must run warning-free even with E_ALL error reporting level and short tags disabled.
For the record, I also did Ruby in 79 and Python in 93...
(Though the Ruby one has trailing space and the Python one has extra delimiters.)
Joined: 11/30/2008
Posts: 650
Location: a little city in the middle of nowhere
I believe this is below 118 characters:(python) I'm not going to enter this because it is almost a direct copy of Ytterboiwhatever's code. I used the "KISS" theory. Undoubtedly it can be improved if "import" is used, but I know too little about python to use it efficiently.
d='1'
for x in range(1001):
if x%7!=0 and any([x%f==0 for f in range(2,x)]):d=d+' '+str(x)
print d