[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

1445.0. "Generate sequence from short C program" by CLT::TRACE::GILBERT (Ownership Obligates) Tue May 14 1991 16:01

Consider the finite sequence:

	0, 0, 2, 9, 24, 50, 90, 147, 224, 324

The problem is to construct the minimum-length C program that
produces this sequence.  The program must "#include stdio.h".
It cannot "use a table, or embed the numbers", whatever those
imprecise terms mean.
T.RTitleUserPersonal
Name
DateLines
1445.1oops, I hear mom callingVMSDEV::HALLYBThe Smart Money was on GoliathTue May 14 1991 18:147
    Is this a math problem or a C problem?
    
    (Meaning is something clever about stdio involved, or is that just to
     level the playing field beteween C implementations that automatically
     include stdio).
    
      John
1445.2all over but the programming? :-)GUESS::DERAMOBe excellent to each other.Tue May 14 1991 21:4310
        The third differences are constant, so the n-th member of
        the sequence can be written as a cubic in n.  Start
        counting with n=0 and you see x and x-1 both divide the
        cubic.  The last factor goes ?,?,1,1.5,2,2.5,3,... and so
        must be x/2.  So the sequence is
        
                 2
        	n (n-1) / 2	n = 0,1,2,3,4,5,6,7,8,9
        
        Dan
1445.3by the way, the #include is superfluous hereGUESS::DERAMOBe excellent to each other.Wed May 15 1991 08:5618
$ cat foo.c
#include <stdio.h>
main(){int i;for(i=0;i<10;i++)printf("%d\n",i*i*(i-1)/2);}
$ wc foo.c
       2       4      78 foo.c
$ cc foo.c
$ a.out
0
0
2
9
24
50
90
147
224
324
$
1445.4bit shorter still..SMAUG::ABBASIWed May 15 1991 15:544
    #include <stdio.h>
    i;m(){for(i=0;i<10;i++)printf("%d\n",i*i*(i-1)/2);}
    
    saves 7 bytes :-)
1445.5need main, not m (DECstation 3100, ULTRIX V4.0)GUESS::DERAMOBe excellent to each other.Wed May 15 1991 18:0418
        re .4,
        
$ cat > foo.c
#include <stdio.h>
i;m(){for(i=0;i<10;i++)printf("%d\n",i*i*(i-1)/2);}
$ cc foo.c
ld:
Undefined:
main
$ wc foo.c
       2       3      71 foo.c
$

	But it didn't work. :-) I had tried "i" instead of "int i"
	inside the function but that didn't work; putting the "i"
	outside the function apparently does, though.

	Dan
1445.6A mathematical code bumSHRIMP::LARYSpukhafte FernwirkungenWed May 15 1991 23:395
    #include <stdio.h>
    i,s;m(){for(s=i=0;i<10;s+=i++)printf("%d\n",i*s);}
    
    saves another byte from .4...

1445.7reminds me of PDP-8 coding dazeALLVAX::JROTHI know he moves along the piersThu May 16 1991 00:1219
    vitus % cat small.c
    i,s;m(){for(;i<10;s+=i++)printf("%d\n",i*s);}
    vitus % wc small.c
           1       1      46 small.c
    vitus % cc -o small -Dm=main small.c
    vitus % small
    0
    0
    2
    9
    24
    50
    90
    147
    224
    324
    vitus %

    - Jim
1445.8two characters?GUESS::DERAMOBe excellent to each other.Thu May 16 1991 00:2427
        When I saw .6 I asked if on VMS the first function was
        called if main was not defined.  But in .7 you compile
        with the flag -Dm=main redefining m to be main.
        
        By that logic why not just do
        
% cat small.c
A
% wc small.c
        1	1	2 small.c
% cc -D'A=main(){int i;for(i=0;i<10;i++)printf("%d\n",i*i*(i-1)/2);}' small.c
% a.out
0
0
2
9
24
50
90
147
224
324
% 
        
        :-)
        
        Dan
1445.9looks like VAXC/vms is different that your compilerSMAUG::ABBASIThu May 16 1991 10:326
    ref .5 (Dan)
    by default if you put a variable outside the function scope it is defaulted
    as int type. at least on VMS implementation of VAXC. 
    also you dont have to use "main" as name of you main function.. again
    on VAX VMS at least.
    /naser