Task: Sum to 100


C++: 
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <set>
#include <map>

using namespace std;

class Expression{
    private:
        enum { NUMBER_OF_DIGITS = 9 }; 
        enum Op { ADD, SUB, JOIN };
        int code[NUMBER_OF_DIGITS];
    public:
        static const int NUMBER_OF_EXPRESSIONS;
        Expression(){
            for ( int i = 0; i < NUMBER_OF_DIGITS; i++ )
                code[i] = ADD;
        }
        Expression& operator++(int){ 
            for ( int i = 0; i < NUMBER_OF_DIGITS; i++ )
                if ( ++code[i] > JOIN ) code[i] = ADD; 
                else break;
            return *this;        
        }
        operator int() const{
            int value = 0, number = 0, sign = (+1);
            for ( int digit = 1; digit <= 9; digit++ )
                switch ( code[NUMBER_OF_DIGITS - digit] ){
                case ADD: value += sign*number; number = digit; sign = (+1); break;
                case SUB: value += sign*number; number = digit; sign = (-1); break;
                case JOIN:                      number = 10*number + digit;  break;
            }
            return value + sign*number;
        }
        operator string() const{
            string s;
            for ( int digit = 1; digit <= NUMBER_OF_DIGITS; digit++ ){
                switch( code[NUMBER_OF_DIGITS - digit] ){
                    case ADD: if ( digit > 1 ) s.push_back('+'); break;
                    case SUB:                  s.push_back('-'); break;
                }
                s.push_back('0' + digit);
            }
            return s;
        }
};
const int Expression::NUMBER_OF_EXPRESSIONS = 2 * 3*3*3*3 * 3*3*3*3;

ostream& operator<< (ostream& os, Expression& ex){
    ios::fmtflags oldFlags(os.flags());
    os << setw(9) << right << static_cast<int>(ex)    << " = " 
       << setw(0) << left  << static_cast<string>(ex) << endl;
    os.flags(oldFlags);
    return os;
}

struct Stat{
    map<int,int> countSum;
    map<int, set<int> > sumCount;
    Stat(){
        Expression expression;
        for ( int i = 0; i < Expression::NUMBER_OF_EXPRESSIONS; i++, expression++ )
            countSum[expression]++;
        for ( auto it = countSum.begin(); it != countSum.end(); it++ )
            sumCount[it->second].insert(it->first);
    }
};

void print(int givenSum){
    Expression expression;
    for ( int i = 0; i < Expression::NUMBER_OF_EXPRESSIONS; i++, expression++ )
        if ( expression == givenSum ) 
            cout << expression;
}

void comment(string commentString){
    cout << endl << commentString << endl << endl;
}

int main(){
    Stat stat;

    comment( "Show all solutions that sum to 100" );
    const int givenSum = 100;
    print(givenSum);

    comment( "Show the sum that has the maximum number of solutions" );
    auto maxi = max_element(stat.sumCount.begin(),stat.sumCount.end());
    auto it = maxi->second.begin(); 
    while ( *it < 0 ) it++;
    cout << static_cast<int>(*it) << " has " << maxi->first << " solutions" << endl;

    comment( "Show the lowest positive number that can't be expressed" );
    int value = 0; 
    while(stat.countSum.count(value) != 0) value++;
    cout << value << endl;

    comment( "Show the ten highest numbers that can be expressed" );
    auto rit = stat.countSum.rbegin();
    for ( int i = 0; i < 10; i++, rit++ ) print(rit->first);

    return 0;
}









Fortran: 









program sumto100
    
    parameter (nexpr = 13122)

    print *
    print *, 'Show all solutions that sum to 100'
    print *
    do i = 0, nexpr-1
        if ( ievaluate(i) .eq. 100 ) then
            call printexpr(i)
        endif    
    enddo
    
    print *
    print *, 'Show the sum that has the maximum number of solutions'
    print *    
    ibest = -1
    nbest = -1
    do i = 0, nexpr-1
        itest = ievaluate(i)
        if ( itest .ge. 0 ) then
            ntest = 0
            do j = 0, nexpr-1
                if ( ievaluate(j) .eq. itest ) then
                    ntest = ntest + 1
                endif
            enddo
            if ( (ntest .gt. nbest) ) then
                ibest = itest
                nbest = ntest
            endif
        endif
    enddo
    print *, ibest, ' has ', nbest, ' solutions'
    print *






    print *
    print *, 'Show the lowest positive number that can''t be expressed'
    print *
    loop: do i = 0,123456789
        do j = 0,nexpr-1
            if ( i .eq. ievaluate(j) ) then
                cycle loop
            endif
        enddo
        exit
    enddo loop
    print *, i
 
    print *
    print *, 'Show the ten highest numbers that can be expressed'
    print *
    ilimit = 123456789
    do i = 1,10
        ibest = 0
        do j = 0, nexpr-1
            itest = ievaluate(j)
            if ( (itest .le. ilimit) .and. (itest .gt. ibest ) ) then
                ibest = itest
            endif
        enddo
        do j = 0, nexpr-1    
            if ( ievaluate(j) .eq. ibest ) then
                call printexpr(j)
            endif    
        enddo
        ilimit = ibest - 1;
    enddo
       
end 

function ievaluate(icode)
    ic = icode
    ievaluate = 0
    n = 0
    ip = 1
    do k = 9,1,-1
        n = ip*k + n
        select case(mod(ic,3))
            case ( 0 )
                ievaluate = ievaluate + n
                n = 0
                ip = 1
            case ( 1 )
                ievaluate = ievaluate - n
                n = 0
                ip = 1
            case ( 2 )
                ip = ip * 10
        end select
        ic = ic / 3                
    enddo
end 

subroutine printexpr(icode)
    character(len=32) s
    ia = 19683
    ib =  6561
    s = ""
    do k = 1,9
        ic = mod(icode,ia) / ib
        ia = ib
        ib = ib / 3
        select case(mod(ic,3))
            case ( 0 )
                if ( k .gt. 1 ) then
                    s = trim(s) // '+'
                endif
            case ( 1 )
                s = trim(s) // '-'
        end select
        s = trim(s) // char(ichar('0')+k)
    end do
    ivalue = ievaluate(icode)
    print *, ivalue, ' = ', s
end