SPOJ 2906. GCD2 with F#

Problem Definition

Frank explained its friend Felman the algorithm of Euclides to calculate the GCD of two numbers. Then Felman implements it algorithm

int gcd(int a, int b)
	if (b==0)
		return a;
		return gcd(b,a%b);

and it proposes to Frank that makes it but with a little integer and another integer that has up to 250 digits.

Your task is to help Frank programming an efficient code for the challenge of Felman.


The first line of the input file contains a number representing the number of lines to follow. Each line consists of two number A and B ( and ).


Print for each pair (A,B) in the input one integer representing the GCD of A and B.

More on this problem is available here.


Greatest common divisor is the largest number that divides both number without any reminder.  Thus, GCD is also called Highest Common Divisor (HCF), or  Greatest Common Factor  (GCF).

The main trick used in the solution lies in the mod' function, which effectively reduces the range of the numbers to , that is:

where is the outcome of mod' function .

Afterwards, a normal gcd function can simply be applied on to compute the result.

//Problem Statement : https://www.spoj.com/problems/GCD2/
open System
let parseLine() =
let line = System.Console.ReadLine().Split()
line.[0] |> int, line.[1] |> string
let rec gcd a b =
match b with
| 0 -> a
| _ -> gcd b (a%b)
// computes b%a
let mod' (b:string) (a:int)=
|> Array.fold (fun acc x -> ((acc*10 + (((int)x)-48))%a)) 0
let rec solveLines currentLine maxLines =
if currentLine < maxLines then
let num1,num2 = parseLine()
match num1,num2 with
| 0,_ -> printfn "%s" num2
| _ ->
mod' num2 num1
|> gcd num1
|> printfn "%d"
solveLines (currentLine+1) maxLines
let solveSpoj2906() =
match Console.ReadLine() |> Int32.TryParse with
| (true, i) when i > 0 -> solveLines 0 i
| _ -> ()
view raw gcd2.fs hosted with ❤ by GitHub

SPOJ 42. Adding Reversed Numbers (ADDREV) with F#

Problem Definition

The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).


The input consists of N cases (equal to about 10000). The first line of the input contains only positive integerN. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.


For each case, print exactly one line containing only one integer – the reversed sum of two reversed numbers. Omit any leading zeros in the output.


// problem definition : https://www.spoj.com/problems/ADDREV/
open System
let parseTuple() : int*int =
let line = Console.ReadLine().Split()
line.[0] |> int , line.[1] |> int
let reverseInt n =
let rec reverseInt' n acc =
let acc' = acc + (n%10)
match n with
| n when n >= 10 -> reverseInt' ((int)(n/10)) (acc'*10)
| n -> acc'
reverseInt' n 0
let solveCase (n1,n2) = reverseInt (reverseInt n1 + reverseInt n2)
let rec solveCases i maxCases =
if i < maxCases then
|> solveCase
|> printfn "%d"
solveCases (i + 1) maxCases // solving next case
let spoj42() =
match Console.ReadLine() |> Int32.TryParse with
| (true, i) when i > 0 -> solveCases 0 i
| _ -> ()
view raw addrev.fs hosted with ❤ by GitHub

More details of this problem is available here