Writing code for three tasks

Ruby, 1356 1268 * 3 = 3804 bytes

if File.file?(?b)
s='P3
50 50
255
'
d=e=0
[153,44,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,6,1,42,1,4,40,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,1,36,1,5,1,4,1,1,34,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,34,1,3,1,5,1,4,1,8,31,5,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,1,25,1,9,1,4,1,8,37,4,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,1,34,1,14,36,63].map{|n|n.times{s+=[" 255 255 255"," 0 0 255"][d];s+=$/if (e+=1)%50==0};d=1-d}
File.write 'blue.ppm',s
elsif File.file?(?a)
puts [["","Mary had a ","little lamb",?,,?,,"Its fleece was white as snow"],["And ","everywhere that ","Mary went",?,,'',"The lamb was sure to go"],["","It followed her to ","school one day",'','',"Which was against the rules."],["","It made the children ","laugh and play",?,,'',"To see a lamb at school"]].map{|a,b,c,d,e,f|[a+b+c+d,c.capitalize+', '+c+d,b.capitalize+c+e,f]*$/}*"

"
`touch b`
else
1971.times{|i|$><<"#{i} "if i.to_s.chars.map(&:to_i).reduce(:+)%17<1}
`touch a`
end

To keep track of the invocation, I'm creating an empty file a on the first run and an empty file b on the second run.

  • The first program uses a certain property of the numbers.
  • The second program uses a bunch of reusable parts of the verses, but that currently only saves 100 bytes over naively printing the entire thing.
  • The third program currently uses just a simple run-length encoding. I'll look into actually encoding the path of the line later.

Python (897 + 814 + 386 = 2097)

Simple self-modifying answer. I'll have to refine the second run later.

def p(x):print x,
map(p,[0]+filter(lambda x:sum(map(int,`x`))==17,range(1971)))
#l="ittle lamb,";M="Mary ";w=M+"went";e="verywhere that "+w;s="chool one day";I="It followed her to s"+s+"\n";j="augh and play";i="It made the children l"+j;print M+"had a l"+l+"\nL"+l+" l"+l+"\n"+M+"had a l"+l+"\nIts fleece was white as snow\n\n"+"And e"+e+",\n"+w+", "+w+",\nE"+e+"\nThe lamb was sure to go\n\n"+I+"S"+s+", s"+s+"\n"+I+"Which was against the rules.\n\n"+i+",\nL"+j+", l"+j+",\n"+i+"\nTo see a lamb at school"
#print>>file("blue.ppm","w"),"P3 50 50 255"+' '.join(" %i"%(255-255*any(a<y<b and x==c or y==d and e<x<f for a,b,c,d,e,f in[(2,48,2,47,2,37),(9,48,36,9,27,37),(9,41,28,40,9,29),(0,41,10,1,9,49),(0,37,48,36,14,49),(2,37,15,3,2,16)]))*2+" 255"for x in range(50)for y in range(50))
import sys;x=file(sys.argv[0],"r+");y='#'.join(x.read().split('#')[1:]);x.seek(0);x.truncate();x.write(y)

Additionally, if run for the fourth time, it safely degrades to a NOP:

'.join(x.read().split('#')[1:]);x.seek(0);x.truncate();x.write(y)

C++, 1204 * 3 = 3612 bytes

Compiler used - GCC 4.9.2 with -std=c++14 flag

#include<bits/stdc++.h>
int i,j,c;
using namespace std;int main(){fstream f("F"),g("G");if(!f){while(i<=1970){j=i,c=0;while(j)c+=j%10,j/=10;if(c==17||i==0)cout<<i<<" ";i++;}ofstream f("F");}else if(f&&!g){cout<<R"(Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow

And everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go

It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.

It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school)";ofstream g("G");}else{ofstream F("blue.ppm");F<<"P3 50 50 ";vector<int> L={1,2,4,10,13,16,19,25,28,43,76,103,109,127,190,460};vector<string> R={"1","?","43","10","6","161=","1","14","39","10","4","1514121<","1","151412","33","10","12","1315141;","1","13151417","30","10","1","1514171","7",":1814171","36","01","1","41","11",";191","35","01","1",">"},C={"255","0"};string r;i=j=0;while(i<R.size()){stringstream S(R[i]);S>>c;while(c--)r+=R[i+1];i+=2;}i=0;while(i<r.size()){c=L[r[i]-48];while(c--){F<<C[j]<<" ";}j=1-j;i++;}}}

Explanation -:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    fstream f( "F" ), g( "G" ); // Creating empty files so that the program can decide when to run which task

    /**************************** TASK 1 ****************************/

    if ( !f ) // Print all the numbers within the range whose sum of digits is 17
    {
        for ( int i = 0; i <= 1970; i++ )
        {
            int j = i, c = 0;
            while ( j )
            {
                c += j % 10, j /= 10;
            }
            if ( c == 17 || i == 0 )
            {
                cout << i << " ";
            }
        }       
        ofstream f( "F" );
    }

    /**************************** TASK 2 ****************************/

    else if ( f && !g )  // Print the second task as it is using raw literals
    {
        cout << R"(Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow

And everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go

It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.

It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school)";

        ofstream g( "G" );
    }

    /**************************** TASK 3 ****************************/

    else // Third task using Run-Length encoding
    {
        ofstream F( "blue.ppm" );
        F << "P3 50 50 ";

        /*
        L = set of all the lengths found during run-length encoding.
        R = vector containing the run-length encoding of "255" and "0" alternatingly.
                {1,"?") means 1 times print '255'/'0' "x" number of times where x = L[ASCII(?)-48] = L[15] = 469
        C = vector containing the two strings to be printed alternatingly.
        */

        vector<int> L = {1, 2, 4, 10, 13, 16, 19, 25, 28, 43, 76, 103, 109, 127, 190, 460};
        vector<string> R = {"1", "?", "43", "10", "6", "161=", "1", "14", "39", "10", "4", "1514121<", "1", "151412", "33", "10", "12", "1315141;", "1", "13151417", "30", "10", "1", "1514171", "7", ":1814171", "36", "01", "1", "41", "11", ";191", "35", "01", "1", ">"};
        vector<string> C = {"255", "0"};
        string r; // Contains the normal/expanded version of R
        i = j = 0;

        while ( i < R.size() ) // Expanding "R" into normal form and putting it into "r"
        {
            stringstream S( R[i] );
            S >> c; // Convert R[i] to its integer representation and put it in "c" with the help of stringstream.
            while ( c-- )
            {
                r += R[i + 1];
            }
            i += 2;
        }

        i = 0;
        while ( i < r.size() )
        {
            c = L[r[i] - 48];  // "c" is the number of times the current character has to be printed consecutively.
            while ( c-- )
            {
                F << C[j] << " ";
            }
            j = 1 - j; // Alternate "255" and "0"
            i++;
        }
    }
}