Joined: 4/25/2004
Posts: 615
Location: The Netherlands
I was kind of busy getting my internet back up actually :p But I am curious as to what you used. Some php code I don't know about or a shorter way to determine primes... When does the code golf end anyways?
qfox.nl
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
qFox wrote:
I was kind of busy getting my internet back up actually :p But I am curious as to what you used. Some php code I don't know about or a shorter way to determine primes...
If you shrink your code to a minimum and it reaches the same length as mine, it's likely the same algorithm… otherwise, not. I suppose you know these: ― <?php does not require a matching ?>. These two are just metacommands that change between "print-everything" and "interpret PHP syntax" modes. ― PHP syntax is similar to C syntax. Have a look at C entries in similar contests to get ideas. I don't know if you have experience with obfuscated programming or not. (what's the matter with IOCCC btw?) Too bad PHP does not have stuff like list comprehensions, and things like create_function() are just a bit too long words to even consider.
OmnipotentEntity wrote:
challenge closes at 7 Jan 7:00AM GMT.
Joined: 4/25/2004
Posts: 615
Location: The Netherlands
Well skipping the ?> only explains two bytes. I must be missing something. I (think I) know about obfuscating php enough that obfuscating the submission beyond the obvious, would make things longer. I guess I'll learn something in two days :)
qfox.nl
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
A few basic things I have learned about short C code: ― Embed the pre- and post-increment in expressions that have another purpose. For example, instead of for(int a=1; a<100; ++a), say for(int a=0; ++a<100; ), or for(int a=0; a++<99; ). Decide whether a post-increment or a pre-increment serves you better. ― Instead of if()else, use && and || or ?: , whenever possible. (Which is almost always.). Also check whether mathematical operators, such as + or * would work in your favor. Be aware of non-obvious function return values. In PHP, print always returns (int)1. For example, if(a==0) c; can be converted into a||c. And if(a) b; can be converted into a&&b. And if(a) { b; } else { c; } can be converted into a?b:c. This is assuming that b and c are expressions and not statements. ― Instead of while(), use for(). It's never a loss, and usually it's a win. For example, a(); while(b < 6) c(); can be converted into for(a(); b<6; c()); This saves one character. ― Reuse variables. For example, if you use argc to capture the number of commandline arguments, reuse it as a loop iteration variable. Note that it does not need to be named argc. ― Be aware of shorter but equivalent commands. In PHP, print and echo do the same thing, but echo is shorter, but is a statement, whereas print is a function. Functions can be used in expressions, but statements cannot.
Joined: 4/25/2004
Posts: 615
Location: The Netherlands
I didnt know, or at least think about, embedding the increment in the for condition, that saves 4 bytes. Afaik PHP treats ++$i and $i++ the same (unlike C etc..). I see the point of the ternary operator except that i need to break, which would be impossible. And this all still only explains half your gain :p -- (by now I completely understand how :p)
qfox.nl
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
qFox wrote:
Afaik PHP treats ++$i and $i++ the same (unlike C etc..).
Incorrect.
Tub
Joined: 6/25/2005
Posts: 1377
I win.
<?php echo basename(__FILE__);
works for any properly named file. :p got my php version down to 74 with the full <?php open tag, so it's 71 or 70 with <? (do you need a space after the short opening tag?) still, there seems to be a smarter way.. edit: bingo.
 ~/files/source/codegolf> php primes.php | md5sum ; wc -c primes.php
e5520566aa3e1456e05d1f17d91ab9d4  -
66 primes.php
m00
HHS
Active player (286)
Joined: 10/8/2006
Posts: 356
Bisqwit wrote:
Feh, readibility would be nice. Also, what is the character encoding you are supposing? The exact byte values of your characters matter to a great deal I suppose and converting the characters into bytes, depends on the character encoding used. I can make an educated guess, but it would be better if it came from the horse's mouth.
It's Windows-1252. The hex code for the ASM file is as follows. 64 62 20 31 39 31 2C 30 2C 32 2C 22 57 B8 31 20 AB 33 C0 B9 1E 22 2C 30 2C 22 41 3B C8 74 22 2C 38 2C 22 50 99 F7 F1 4A 58 78 22 2C 31 38 2C 22 49 E2 F0 40 3D E9 22 2C 33 2C 22 72 E7 B0 24 AA 5A B4 22 2C 39 2C 22 CD 21 C3 50 99 B1 22 2C 37 2C 22 F7 F1 4A 58 78 E7 33 DB 50 8B F7 99 B1 22 2C 31 30 2C 22 F7 F1 50 8A C2 22 2C 31 32 2C 22 30 AA 58 85 C0 75 F0 57 4F 8A 22 2C 34 2C 22 86 22 2C 35 2C 22 88 22 2C 34 2C 22 46 3B F7 72 F4 5F B0 20 AA 58 EB BE 22 Here's the original, more readable (and longer) version, in MASM. .model tiny .code org 256 mov di,512 push di mov ax,2031h stosw xor ax,ax nbrloop: mov cx,30 primeloop: inc cx cmp cx,ax jz samenbr push ax cwd div cx dec dx pop ax js notprime samenbr: dec cx loop primeloop nextnbr: inc ax cmp ax,1001 jb nbrloop mov al,36 stosb pop dx mov ah,9 int 21h ret notprime: push ax cwd mov cl,7 div cx dec dx pop ax js nextnbr xor bx,bx push ax mov si,di div10lp: cwd mov cl,10 div cx push ax mov al,dl or al,48 stosb pop ax test ax,ax jnz div10lp push di revnbrlp: dec di mov al,[si] xchg al,[di] mov [si],al inc si cmp si,di jb revnbrlp pop di mov al,32 stosb pop ax jmp nextnbr end
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Btw, the POV-Ray short code contests are more fun because the results are more impressive, at least visually. For example, see what can be done with 256 bytes of POV-Ray scripting language: http://ozviz.wasp.uwa.edu.au/~pbourke/exhibition/scc4/final/
Joined: 7/26/2006
Posts: 1215
Fuck it's after the deadline but I didn't see this thread until now. Sending PM asap. Edit: Oh nevermind. my code is kind of long, and I was aiming for readablity and fastest execution, not fewest characters. (28 seconds to execute to 1000000 on a 1.8GHz laptop) Edit2: quickly shrunk it anyway and PM'ed
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
When will the results table be updated? I sent an improvement (by 2 characters) of my entry two days ago but it hasn't been updated...
Player (36)
Joined: 9/11/2004
Posts: 2630
Updating results now. I apologize for the delay.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Joined: 4/25/2004
Posts: 615
Location: The Netherlands
Btw, if you like these programming challenges (not so much the code golf, but to write something with very specific output): http://projecteuler.net/ Has a number of challenges where you have to enter the output on the site and you get ranked by the number of proper solutions you supply etc. Still haven't had the time to get into it myself, but I will... :)
qfox.nl
Player (36)
Joined: 9/11/2004
Posts: 2630
This is a dump of the code received for the contest. I will write up some commentary later on, if anyone wants to write up commentary for their own code, let me know. (or, you know, just post it here) 46 characters, by DK64 and DarkKobold, written in Matlab
x=1:1e3;fprintf('%d ',x(~isprime(x)&mod(x,7)))
50 characters, by flagitious, written in Ruby
$><<((s=1)..1e3).select{|q|-2[q%7]>s%q%s*=q*q}*' '
56 characters, by OmnipotentEntity, written in Perl
print"1 ".join' ',grep{$_%7&&(1x$_)=~/^(11+)\1+$/}2..1e3
59 characters, by pauli, written in Ruby
$><<[1,(4..1E3).select{|i|i%7>0&&' '*i=~/^(..+?)\1+$/}]*' '
60 characters, by qFox & Bisqwit, written in PHP
1<?for(;--$j>1?1+($i%$j||$i%7&&$j=print" $i"):1e3>$j=$i++;);
61 characters, by Bisqwit, written in Perl
for(;$n++<1e3;){($s.=1)=~/^1$|^(11+?)\1+$/&&$n%7&&print"$n "
62 characters, by Tub, written in PHP
1<?for(;1001>$d=++$i;)for(;--$d>1;$i%$d||$i%7&&$d=print" $i");
69 characters, by Bisqwit, written in PHP
1<?for(;$n++<1e3;preg_match('/^(11+?)\1+$/',$s.=1)*$n%7&&print" $n");
75 characters, by Pauli, written in PHP
1<?for($n=3;$n++<1E3;)for($i=1;++$i<$n;)if($n%$i<1&&$n%7&&print" $n")break;
76 characters, by Blublu, written in Perl
$i=1;do{print"$i "if$i%7>0&$n!=$i;$i++;$n=2;$n++until$i%$n==0}while($i<1001)
79 characters, by Bisqwit, written in Javascript
for(s='1',a=n=1;n++<1e3;){s+=1;if(s.match(/^(11+?)\1+$/)&&n%7)a+=' '+n}alert(a)
79 characters, by Bisqwit, written in Ruby
(1..1000).each{|n|v=n<2;(2..n-1).each{|k|v=1if n%k<1};print n,' 'if n%7>0and v}
82 characters, by Warp, written in C (compile without optimizations)
c=1,r;p(v){r=v<c?c%v?p(v+1):c%7:c<2;}main(){while(p(2),r?printf("%i ",c)<5:1)c++;}
83 characters, by LagDotCom, written in Python
print 1,
for i in range(1001):
 if i%7and[n for n in range(2,i)if not i%n]:print i,
84 characters, by Bisqwit, written in C
main(i,j){i=j=printf("1");for(;--j>1?i%j?1:i%7?j=1,printf(" %d",i):1:1e3>(j=i++););}
84 characters, by qFox, written in PHP
1<?for($i=2;$i<1001;++$i)for($j=2;$j<$i;++$j)if($i%$j<1&&$i%7>0){echo" $i";break;}?>
87 characters, by Mueen Nawaz, written in Python
print 1," ".join(str(i) for i in range(1001)if any(not i%k for k in range(2,i))and i%7)
96 characters, by Andymac, written in Python
d='1'
for i in range(1001):
 if i%7>0and any([i%n==0for n in range(2,i)]):d=d+' '+str(i)
print d
96 characters, by POV-Ray Mascot, written in Haskell
k=[x|x<-[1..1000],not(all(>0)[mod x y|y<-[2..x-1]])&&mod x 7>0];main=putStr$unwords$map show$1:k
101 characters, by Bisqwit, written in QBASIC
?"1";:FOR n=1TO 1000
IF n MOD 7THEN
FOR c=2TO n\2:IF n MOD c=0THEN?STR$(n);:EXIT FOR
NEXT
END IF
NEXT
102 characters, by Bisqwit, written in GW-BASIC
1?"1";:FOR n=1TO 1000
2IF n MOD 7=0THEN 5
3p=n:FOR c=2TO p\2:IF p MOD c=0THEN?STR$(p);:p=1
4NEXT
5NEXT
105 characters, by Pauli, written in Python
o='1'
for p in filter(lambda n:n%7>0 and any([n%i<1 for i in range(2,n)]),range(1001)):o+=' %d'%p
print o
118 characters, by YtterbiJum, written in Python
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)
145 characters, by Pauli, written in C#
using System;class P{static void Main(){Console.Write(1);for(int n=2;n++<1E3;)for(int i=1;++i<n;)if(n%i<1&&n%7>0){Console.Write(" "+n);break;};}}
149 characters, by qFox, written in java
class B{public static void main(String[] a){int i,j;System.out.print(i=1);for(;i++<1e3;)for(j=1;++j<i;System.out.print(i%7>0&&i%j<1?" "+(j=i):""));}}
150 characters, by Blublu, written in java
class r{public static void main(String[]a){String s="1";for(int n,i=1;i<1001;i++)for(n=2;n<i;n++)if(i%n==0&i%7>0){s+=" "+i;n=i;}System.out.print(s);}}
260 characters, by YtterbiJum, written in Whitespace (transliterated, spaces become A and tabs become B, newline becomes C, remove the newlines I inserted to stop the page from breaking)
AAABCBCABAAAACAAABAACBBACAAACAAAACBBBAAABBBCBABBCBAAA
CAAABCAAABACBBACAAABCAAAACBBBAAABCBBBBABBCBABBCAAABCACABB
BAAABCBAAABBAAAABCBBBAAAACBBBBAABCBAAACCACABCCAABBCAAABAA
AAACBCAAAAAACBBBBCABCAAAACAAAACACABBBAAABCBAAABBAAAAACBBB
AAABBBBBABAABCBAABCBABCCACACCAABCCCC
345 characters, by AndyMac, written in LOLCODE (untested, my interpreter didn't understand LOL LOLCATS R 2, but LOLCODE is still not syntactically stable.)
HAI
CAN HAS STDIO?
I HAS A CHEEZEBURGER
I HAS A LOLCATZ
IM IN YR LOOP
UP CHEEZEBURER!!1
LOL LOLCATZ R 2
IM IN YR LOOP
IZ CHEEZEBURGER LEFTOVER LOLCATZ LIEK 0 AND CHEEZEBURGER LEFTOVER 7 NOT LIEK 0?
YARLY
VISIBLE CHEEZEBURGER
GTFO
KTHX
UP LOLCATZ!!1
IZ CHEEZEBURGER LIEK LOLCATZ? GTFO. KTHX
KTHX
IZ CHEEZEBURGER LIEK 1000? GTFO. KTHX
KTHX
KTXHBYE
502, by Pauli, uses Befunge
" 1",,4v              @
       >11p11g8555***`|
       +              1
       1              1
                      g
       g              7
       1              %
       1     v p331   _v
       ^    <          <
             2
             2
            .2
            gp
            1v      p<
            12       2
       ^   _^2       2
           g g       +
           3 >11gv   1
           3 v -1<   g
                     2
             `       2
           ^ _11g22g%|
           ^     p330<
EDIT: Postmortem submissions: 118 characters, by bkDJ, written in C++
#include <iostream>
int main(){for(int n=1,c;n<1001;++n){for(c=2;n>1&&c<n&&n%c++;){}if(n%7&&c-n){std::cout<<n<<" ";}}}
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
You omitted my 83-character C entry which isn't broken:
c=1;p(v){return v<c?c%v?p(v+1):c%7:c<2;}main(){while(p(2)?printf("%i ",c)<5:1)c++;}
Joined: 4/25/2004
Posts: 615
Location: The Netherlands
In all fairness, my collaboration with bisqwit only goes up to a version around 69, I think. After that he used his own method and improved that one.
qfox.nl
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
I have to say, Flagitious's one is the most interesting one, abusing the fact that Ruby uses bignums for math. Then comes the regexp one which I discovered around half-way to the contest, and which I realized that it must be the one that mr. Omni used for his entry. Seems that Pauli also discovered it at some point. Unfortunately, I didn't know Perl syntax well enough to compete reasonably with Omni. My Perl version was just a direct translation of the PHP version I wrote myself. I was well surprised though, how small we got the PHP entry with qFox. In all fairness, the idea of iterating through two variables with a single loop was mine, but I need to recognize qFox because without his insistance, I would not have believed that pursuing the just-loop-and-mod algorithm is worthwhile. In retrospect, implementing our PHP algorithm in GW-BASIC would become:
1?"1";
2j=j-1:IF j<2THEN 4
3IF i MOD j=0AND i MOD 7THEN?STR$(i);ELSE 2
4i=i+1:j=i:IF j<=1e3THEN 2
(100 bytes, requires CRLF. QuickBASIC version is the same except that labels "1" and "3" can be removed and just LF suffices, making it 95 bytes.) However, the tokenized GW-BASIC format is even shorter, 87 bytes:
       0  ff c1 12 01 00 91 22 31  22 3b 0a 13 4a e7 4a ea  ......"1";..J.J.
      10  12 3a 8b 20 4a e8 13 cd  20 0e 2b 00 8b 20 49 20  .:. J... .+.. I
      20  f3 20 4a e7 11 ee 20 49  20 f3 20 18 cd 91 ff 93  . J... I . .....
      30  28 49 29 3b 3a a1 20 0e  18 00 49 e7 49 e9 12 3a  (I);:. ...I.I..:
      40  4a e7 49 3a 8b 20 4a e8  e7 1d 00 00 7a 8a cd 20  J.I:. J.....z..
      50  0e 02 00 00 00 00 1a                              .......
On the other end, the binary-format QuickBASIC file is 567 bytes.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
A fellow named scott in another forum created (unfortunately too late to enter) this solution in J:
}.~.(1-(1 p:i.1001))*((7|i.1001)>0)*i.1001
He estimates that it could be shortened even further.
Post subject: I'm just fooling around with C++ for fun
Editor, Emulator Coder, Expert player (2156)
Joined: 5/22/2007
Posts: 1134
Location: Glitchvania
C++: This is just for fun. It doesn't give exactly the expected output, and even fails to be complied by any C++ compiler. However all the desired numbers (from 1 to 1000 skipping every number prime or divisible by 7) can be generated by the compiler through error messages. WARNING: Don't compile this unless you are using a very powerful PC (eg. that can run smoothly unoptimized bad games like Crysis and even GTA4 under WINE on Linux </lie>) and a robust C++ compiler with correct template-depth settings! Honestly, I've only tested numbers up to 100 by myself using GCC. You may also change the number 1000 in the source to around 100 to save time and compile.
#define T ;};template<int p
#define E {enum{r
#define S struct
{T,int i>S C E=p%i&&C<p,i-1>::r}T>S C<p,1>E=1}T>S C<1,p>E=0}T>S O E=C<p,p-1>::r};O<p-1> o;int x[r|!(p%7)]T>S O<0>{};O<1000> o;
UPDATE: Actually, you don't need main() at all!
<klmz> it reminds me of that people used to keep quoting adelikat's IRC statements in the old good days <adelikat> no doubt <adelikat> klmz, they still do
Post subject: Re: I'm just fooling around with C++ for fun
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
klmz wrote:
However all the desired numbers (from 1 to 1000 skipping every number prime or divisible by 7) can be generated by the compiler through error messages.
I set the range to 40.
bisqwit@chii:~$ g++-4.0 tmp.cc tmp.cc:6: error: expected unqualified-id before '{' token tmp.cc:6: error: template parameters not used in partial specialization: tmp.cc:6: error: 'p' bisqwit@chii:~$ g++-4.1 tmp.cc tmp.cc:6: error: expected unqualified-id before '{' token tmp.cc:6: error: template parameters not used in partial specialization: tmp.cc:6: error: 'p' bisqwit@chii:~$ g++-4.2 tmp.cc tmp.cc:6: error: expected unqualified-id before '{' token tmp.cc:6: error: template parameters not used in partial specialization: tmp.cc:6: error: 'p' bisqwit@chii:~$ g++-4.3 tmp.cc tmp.cc:6: error: expected unqualified-id before '{' token tmp.cc:6: error: template parameters not used in partial specialization: tmp.cc:6: error: 'p'
HTTP 417 EXPECTATION FAILED
bisqwit@chii:~$ g++-3.3 tmp.cc tmp.cc:6: error: syntax error before `{' token tmp.cc:6: error: template parameters not used in partial specialization: tmp.cc:6: error: `p' tmp.cc: In instantiation of `O<1>': tmp.cc:6: instantiated from `O<2>' tmp.cc:6: instantiated from `O<3>' tmp.cc:6: instantiated from `O<4>' tmp.cc:6: instantiated from `O<5>' tmp.cc:6: instantiated from `O<6>' tmp.cc:6: instantiated from `O<7>' tmp.cc:6: instantiated from `O<8>' tmp.cc:6: instantiated from `O<9>' tmp.cc:6: instantiated from `O<10>' tmp.cc:6: instantiated from `O<11>' tmp.cc:6: instantiated from `O<12>' tmp.cc:6: instantiated from `O<13>' tmp.cc:6: instantiated from `O<14>' tmp.cc:6: instantiated from `O<15>' tmp.cc:6: instantiated from `O<16>' tmp.cc:6: instantiated from `O<17>' tmp.cc:6: instantiated from `O<18>' tmp.cc:6: instantiated from `O<19>' tmp.cc:6: instantiated from `O<20>' tmp.cc:6: instantiated from `O<21>' tmp.cc:6: instantiated from `O<22>' tmp.cc:6: instantiated from `O<23>' tmp.cc:6: instantiated from `O<24>' tmp.cc:6: instantiated from `O<25>' tmp.cc:6: instantiated from `O<26>' tmp.cc:6: instantiated from `O<27>' tmp.cc:6: instantiated from `O<28>' tmp.cc:6: instantiated from `O<29>' tmp.cc:6: instantiated from `O<30>' tmp.cc:6: instantiated from `O<31>' tmp.cc:6: instantiated from `O<32>' tmp.cc:6: instantiated from `O<33>' tmp.cc:6: instantiated from `O<34>' tmp.cc:6: instantiated from `O<35>' tmp.cc:6: instantiated from `O<36>' tmp.cc:6: instantiated from `O<37>' tmp.cc:6: instantiated from `O<38>' tmp.cc:6: instantiated from `O<39>' tmp.cc:6: instantiated from `O<40>' tmp.cc:6: instantiated from here tmp.cc:6: error: creating array with size zero (`0') tmp.cc:6: confused by earlier errors, bailing out
HTTP 416 RANGE NOT SATISFIABLE
Joined: 7/2/2007
Posts: 3960
I remember hearing of an entire text adventure game that was implemented through compiler errors and warnings.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Post subject: Re: I'm just fooling around with C++ for fun
Editor, Emulator Coder, Expert player (2156)
Joined: 5/22/2007
Posts: 1134
Location: Glitchvania
Bisqwit wrote:
I set the range to 40.
The default depth (500) of GCC 3.4.5 is OK for numbers up to 100 (list shortened):
klmz wrote:
$ gcc matp2.cpp matp2.cpp:4: error: expected unqualified-id before '{' token matp2.cpp:4: error: template parameters not used in partial specialization: matp2.cpp:4: error: `p' matp2.cpp: In instantiation of `O<1>': matp2.cpp:4: instantiated from `O<2>' matp2.cpp:4: instantiated from `O<3>' matp2.cpp:4: instantiated from `O<4>' matp2.cpp:4: instantiated from `O<5>' matp2.cpp:4: instantiated from `O<6>' matp2.cpp:4: instantiated from `O<7>' matp2.cpp:4: instantiated from `O<8>' matp2.cpp:4: instantiated from `O<9>' ... matp2.cpp:4: instantiated from `O<98>' matp2.cpp:4: instantiated from `O<99>' matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<4>': matp2.cpp:4: instantiated from `O<5>' matp2.cpp:4: instantiated from `O<6>' matp2.cpp:4: instantiated from `O<7>' matp2.cpp:4: instantiated from `O<8>' matp2.cpp:4: instantiated from `O<9>' ... matp2.cpp:4: instantiated from `O<98>' matp2.cpp:4: instantiated from `O<99>' matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<6>': matp2.cpp:4: instantiated from `O<7>' matp2.cpp:4: instantiated from `O<8>' matp2.cpp:4: instantiated from `O<9>' ... ... matp2.cpp:4: instantiated from `O<98>' matp2.cpp:4: instantiated from `O<99>' matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<95>': matp2.cpp:4: instantiated from `O<96>' matp2.cpp:4: instantiated from `O<97>' matp2.cpp:4: instantiated from `O<98>' matp2.cpp:4: instantiated from `O<99>' matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<96>': matp2.cpp:4: instantiated from `O<97>' matp2.cpp:4: instantiated from `O<98>' matp2.cpp:4: instantiated from `O<99>' matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<99>': matp2.cpp:4: instantiated from `O<100>' matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp: In instantiation of `O<100>': matp2.cpp:4: instantiated from here matp2.cpp:4: error: creating array with size zero (`0') matp2.cpp:4:126: warning: no newline at end of file
The desired numbers:
matp2.cpp: In instantiation of `O<1>': matp2.cpp: In instantiation of `O<4>': matp2.cpp: In instantiation of `O<6>': matp2.cpp: In instantiation of `O<8>': matp2.cpp: In instantiation of `O<9>': matp2.cpp: In instantiation of `O<10>': ... matp2.cpp: In instantiation of `O<93>': matp2.cpp: In instantiation of `O<94>': matp2.cpp: In instantiation of `O<95>': matp2.cpp: In instantiation of `O<96>': matp2.cpp: In instantiation of `O<99>': matp2.cpp: In instantiation of `O<100>':
EDIT: Hmm.
<klmz> it reminds me of that people used to keep quoting adelikat's IRC statements in the old good days <adelikat> no doubt <adelikat> klmz, they still do
Post subject: I should have found this thread earlier...
Editor, Emulator Coder, Expert player (2156)
Joined: 5/22/2007
Posts: 1134
Location: Glitchvania
In C (71 bytes):
n=0;main(c){for(;++n<1001;n%7*--c&&printf("%d ",n))for(c=n;--c&&n%c;);}
EDIT 1: Or (71 bytes):
n=0;main(c){for(;n<1000;n%7*--c&&printf("%d ",n))for(c=++n;--c&&n%c;);}
EDIT 2: 1-character shorter (70 bytes):
n=0;main(c){for(;--c&&n%c||n%7*--c&&!printf("%d ",n)||(c=++n)<1001;);}
EDIT 3: Or (70 bytes):
n=0;main(c){while(--c&&n%c||n%7*--c&&!printf("%d ",n)||(c=++n)<1001);}
EDIT 4: Or (70 bytes):
n=0;main(c){while(--c&&n%c||(n%7*--c&&printf("%d ",n),c=++n,n<1001));}
I recommend the 3rd one (EDIT 2).
<klmz> it reminds me of that people used to keep quoting adelikat's IRC statements in the old good days <adelikat> no doubt <adelikat> klmz, they still do
Post subject: Re: I should have found this thread earlier...
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
klmz wrote:
n=0;main(c){for(;--c&&n%c||n%7*--c&&!printf("%d ",n)||(c=++n)<1001;);}
Since printf() returns the number of characters written, and since the output ends when "1000 " has been written, you can (as I did) use printf(...)<5 as the condition for continuation, saving several characters. Btw, you seem to assume that 'c' has a certain value at the beginning of the program. Do I guess correctly that giving any parameters to the program will break it?
Post subject: Hmm, interesting idea...
Editor, Emulator Coder, Expert player (2156)
Joined: 5/22/2007
Posts: 1134
Location: Glitchvania
Warp wrote:
Since printf() returns the number of characters written, and since the output ends when "1000 " has been written, you can (as I did) use printf(...)<5 as the condition for continuation, saving several characters.
Surprisingly, the double-for-loop one results in shorter code using the 'printf()' return value trick: 68 characters:
n=0;main(c){for(;n%7*--c?printf("%d ",n)<5:1;)for(c=++n;--c&&n%c;);}
69 characters:
n=0;main(c){for(;--c&&n%c||(n%7*--c?printf("%d ",n)<5:1)&&(c=++n););}
However, this trick only works with ranges 10^N :(
Warp wrote:
Btw, you seem to assume that 'c' has a certain value at the beginning of the program. Do I guess correctly that giving any parameters to the program will break it?
The only chance to crash the code would be to compute 'n%c' with 'c' = 0, but '--c&&' prevents that. The only period when 'c' is undefined by the program is the first time evaluating the conditional clause with 'n' = 0 until reaching '(c=++n)', while 'printf("%d ",n)' can be skipped since '0%any_integer_but _0' == 0, no matter what value 'c' is. EDIT: Well, I've read some ANSI C stuff and found that global variables can be implicitly initialized with 0: 66 characters:
n;main(c){for(;n%7*--c?printf("%d ",n)<5:1;)for(c=++n;--c&&n%c;);}
<klmz> it reminds me of that people used to keep quoting adelikat's IRC statements in the old good days <adelikat> no doubt <adelikat> klmz, they still do