Solution of Folklore

Since any number can be written as a sum of powers of 2, it is enough to print the powers of 2 from 20 to 220.

C solution (Marius Gavrilescu)

    1 #include<stdio.h>
    2 
    3 int main(void){
    4     int i;
    5     puts("20");
    6     for(i = 0 ; i < 20 ; i++)
    7         printf("%d ", 1 << i);
    8     return 0;
    9 }

Perl solution (Marius Gavrilescu)

    1 #!/usr/bin/perl
    2 use v5.14;
    3 use warnings;
    4 
    5 $, = ' ';
    6 say 20;
    7 say map { 1 << $_ } 0 .. 19;

Python solution (Sergiu Puscas)

    1 print '20\n' + ' '.join([str(2**x) for x in range(0, 20)])
Questions?

Sponsors Gold