Zerø Wind Jamie Wong

SPOJ Problem 1

When I saw that Thor had done solutions to the first few problems of Project Euler in many many languages, I thought “Hey! That’s a good idea!” but didn’t want to copy exactly.

So instead I’m doing solutions to some problems on SPOJ. SPOJ is an online judge system full of algorithmic problems. This is one of the things that Hanson Wang did to get as good as he is. The solutions I’ll be providing here are going to be very simple problems, so don’t expect any magic. The beautiful thing about SPOJ is the sheer number of languages it will judge. I figured this was the perfect playground to make sure my code worked in all the languages I try.

Problem: Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Solutions - In order of frequency that I use the language

C++

//TEST AC - CPP (g++)
#include <iostream>
using namespace std;

int main() {
  int n;
  while(1) {
    cin >> n;
    if (n == 42) break;
    cout << n << endl;
  }
  return 0;
}

C99

//TEST AC - (gcc C99)
#include <stdio.h>
int main() {
  int n;
  while(1) {
    scanf("%d",&n);
    if (n == 42) break;
    printf("%d\n",n);
  }
  return 0;
}

php

<?
//TEST AC - PHP
while(1) {
  fscanf(STDIN,"%d",$n);
  if ($n == 42) break;
  print "$n\n";
}
?>

Python

#TEST AC - Python
while 1:
  num = input()
  if (num == 42):
    break
  print num

Bash

#!/bin/bash
# TEST AC - BASH

while true; do
  read n
  if [ $n -eq 42 ]; then
    break
  fi
  echo "$n"
done

Ruby

#TEST AC - Ruby
while 1
  n = gets.to_i
  if n == 42
    break
  end
  puts n
end

Java

//TEST AC - Java
import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    while(true) {
      int n = in.nextInt();
      if (n == 42) break;
      System.out.println(n);
    }
  }
}

Perl

#TEST AC - Perl
while (1)
{
  $n = <STDIN>;
  if ($n == 42) {
    last;
  }
  print $n
}

C#

//TEST AC - C# (gmcs + Mono)
using System;
class WelcomeCSS {
  static void Main() {
    while(true) {
      int n;
      n = int.Parse(Console.ReadLine());
      if (n == 42) break;
      Console.WriteLine(n);
    }
  }
}

GNU Pascal

{TEST AC - GPC Pascal}

program TEST;

var n:integer;

begin
  while true do
    begin
      readln(n);
      if n = 42 then begin
        break;
      end;
      writeln(n);
    end;
end.

You can see Thor’s Project Euler solutions here: Derek Thurn’s Website - Project Euler

If you liked reading this, you should follow me on twitter, take a look at other blog posts by me, or maybe even come work with me at Figma!


Zerø Wind Jamie Wong