Square Triple

February 14, 2020

We have a simple homework problem today:

Given a list of distinct integers, find all triples (x y z) where x, y and z are in the list and x * y = z².

Your task is to find the list of triples. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Removing Spaces

February 11, 2020

We have a simple task today, with dozens of potential solutions:

Write a program to remove all spaces from a string.

Your task is to write a program to remove all spaces from a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

My Mailbag

February 7, 2020

I had two interesting emails from readers this week. I’ll discuss both of them, but suppress the names of the writers; they can identify themselves in the comments below if they wish.

One writer saw Johnny Ball’s video about Russian Multiplication on Numberphile and suggested it would make a good exercise. Indeed it would; in fact, we have already done it twice ([1] [2]).

Another writer suggested that the compose function in the Standard Prelude should return the identity function if called with no arguments, rather than reporting an error. That is, of course, correct; my apologies for the bug.

Your task is to write programs to perform russian multiplication and compose functions, as suggested above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Password Generator

February 4, 2020

Many web sites require passwords that have particular combinations of various types of characters; for instance, a web site might require a password that has at least ten characters, including at least one each from the sets of lower-case letters, upper-case letters, digits, and special characters.

Your task is to write a program that generates passwords according to specifications that you define. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Anna’s Homework

January 31, 2020

Let’s help Anna with her homework:

Given an array of n elements, find an element x that appears in the array at least n/3 times, or indicate that no such element exists. Your program may take no more than O(n) time and O(n) space.

Your task is to write a program to solve Anna’s homework problem. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Non-Abundant Sums

January 28, 2020

It’s been a long time since we did an exercise from Project Euler; here is number 23:

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

Your task is to solve Project Euler 23; in the spirit of Project Euler, please do not display the solution. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Mertens’ Conjecture

January 24, 2020

Dr Holly Krieger discussed Mertens’ Conjecture on Numberphile yesterday:

Mertens’ Conjecture: The absolute value of the Mertens function M(n), computed as the sum for k from 1 to n of the Moebius function μ(k), is less than the square root of n. The Moebius function μ(n) is (-1)^k, where k is the number of prime factors of n, but 0 if n has any repeated prime factors.

The conjecture has been proved false, though no counter-examples are known. You can read more about Mertens’ Conjecture at MathWorld or Wikipedia.

Your task is to write a program to compute Mertens’ function M(n) and use it to explore some of the sequences at OEIS. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Mini Markdown

January 21, 2020

Today’s exercise is an interview question. The situation is an interviewee at home, sharing his screen with an interviewer at his office. The interviewee has one hour to write the following program while the interviewer watches:

Write a program that takes a text file as input and writes an html file as output. The input consists of one or more paragraphs (maximal lines of text separated by blank lines). The following elements of John Gruber’s Markdown language are supported:

– ###### up to six levels of headings

– unordered lists introduce by a dash (like this paragraph)

– plain text paragraphs

Your task is to write a mini-Markdown processor; you have one hour to complete it. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Coprime Numbers

January 17, 2020

This exercise comes from CodeForces:

Given two natural numbers lo < hi, find a triple (a, b, c) such that a is coprime to b, b is coprime to c, and a is not coprime to c, or indicate that no such triple exists. If there is more than one such triple, you may return any of them. Two numbers are coprime if their greatest common divisor is 1. Lo and hi will be less than 1018 and differ by no more than 50.

Your task is to find a triple satisfying the conditions above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Newbie Question

January 14, 2020

We have something different today.

Although most of my readers are accomplished, savvy programmers, (or at least those who are confident enough to comment) I will always include content designed for beginners; it’s part of the DNA of the blog, and sometimes brings in new readers. Thus, I spend a lot of time in beginner-programmer online forums. Today’s question at Reddit caught my eye:

Newbie question: What’s the difference between a “loop” and a “function”?

Sorry if this is answered somewhere but I am completely new to programming.

What is the difference between a function and a loop, when both of them can be used more than once?

The posting history of the person who asked that question indicates that he has recently been expelled from college (his GPA wasn’t great) and that

In the meantime I just started a computer programming course online. Was never really interested in it but everyone says it’s a skill you should have so I figured I’ll give it a shot. Plus it’s free so there’s that.

Your task is to answer the newbie question; if you answer on Reddit, please also leave your answer here, with a pointer to your Reddit posting. When you are finished, you are welcome to read my answer or discuss the exercise in the comments below.

Pages: 1 2