Gossip Language Tutorial

In this tutorial, we will use the Gossip programming language to solve some of the problems from the first few Euler problems.

Problem 1: Multiples of 3 or 5

Problem: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Solution:

declare n = 1000;
declare s = 0;
    
declare i = 1;
    
while i<n do {
    if i%3==0 || i%5==0 then 
        {assign s = s + i;
    }
    assign i = i+1;
};
print(s);

Problem 2: Even Fibonacci numbers

Problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Solution:

declare sum = 0; 
declare i = 0; 
declare j = 1; 
declare k = 0; 
while j<=4000000 do {
    assign k = i + j; 
    assign i = j; 
    assign j = k; 
    if j%2==0 then assign sum = sum + j; 
    }; 

print(sum);

Problem 3: Largest prime factor

Problem: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

Solution:

declare n = 600851475143;
declare maxpr = 0;
while n%2==0 do {
    assign maxpr = 2;
    assign n = n/2;
    };

declare i = 3;
while i*i<=n do {
    while n%i==0 do {
        assign maxpr = i;
        assign n = n/i;
    };
    assign i = i+2;
};

if n>2 then print(n); else print(maxpr);;

Problem 4: Largest palindrome product

Problem: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.

Solution:

deffunct check(n){
    declare m = 0; 
    declare r = n;
    declare q = 1; 
    while r>0 do {
        assign m = m*q+r%10; 
        assign r = r - r%10; 
        assign r = r/10; 
        if q==1 then assign q = 10;}; 
        functret(m);};

declare n  = 3; 
declare m = 0; 
declare i = 999;
declare j = 999;
while i>= 100 do { 
    assign j = 999; 
    while j>= 900 && m==0 do {
        declare prod = i*j;
        declare a = callfun check(prod);;
        if prod == a then {
            assign m = 1; 
            assign n = prod;
            }
            assign j = j -1; 
            }; 
        assign i = i - 1;
    }; 
print(n);

Problem 5: Smallest multiple

Problem: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Solution:

declare sum = 2; 
declare i = 1; 
declare j = 2; 
declare k = 0; 
declare x = [2,3,5,7,11,13,17,19];
declare y = [4,2,1,1, 1, 1, 1, 1];
declare z = 1;
declare c = 0;
for i in range(0, 7) do{
    assign c = 0;
    while c < y[i] do {
        assign z = z*x[i]; 
        assign c = c+1;
        };
};

print(z);

That concludes this tutorial on using Gossip to solve some of the Euler problems. We encourage you to try solving the remaining problems on your own.