Find The Difference

January 15, 2019

Today’s question comes from a programming student:

You are given two strings s and t that consist only of lower-case letters. String t was created by adding one letter chosen at random to string s, then shuffling the resulting string. Find the letter that was added to t. For instance, the difference between the two strings “abcdef” and “bfxdeac” is the character “x”.

Your task is to write a program to find the random letter that was added to the 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.

Advertisements

Pages: 1 2

Parallel Assignment

January 11, 2019

Recently, I was writing a program where I needed parallel assignment; the details of my program don’t matter. Some languages provide parallel assignment natively; for instance, Python:

Python 2.7.13 (default, Mar 13 2017, 20:56:15)
[GCC 5.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a,b,c = 1,2,3
>>> print a,b,c
1 2 3
>>> a,b,c = b,c,a
>>> print a,b,c
2 3 1

You can do something similar in C (I think — I’m not sufficiently expert in C to be certain this works):

#define SWAP(x, y) { typeof(x) t = x; x = y; y = t }

Your task is to add parallel assignment to your favorite programming language. 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

Perfect Totient Numbers

January 8, 2019

A perfect number is a number for which the sum of its proper divisors is equal to the number; for instance, 28 is a perfect number because it is the sum of its proper divisors 1 + 2 + 4 + 7 + 14 = 28.

A perfect totient number (A082897) is a number for which the sum of its iterated totients is equal to the number. For instance, 327 is a perfect totient number; its iterated totients are φ(327) = 216, φ(216) = 80, …, and the sum of 216 + 80 + … = 327. I love number theory; it’s totally useless, and endlessly fascinating, as you will see if you follow the links on OEIS.

Your task is to compute the perfect totient numbers less than 10,000. 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

[ I worked half-time during December, still suffering fatigue, then during the Christmas break I visited my daughter in Houston; I’m back at work full-time now. Thanks to all for your good wishes. ]

Numberphile has a short episode about self-locating strings in π; for instance, if you ignore the part before the decimal point and number the digits of π starting from zero, the sixth digit of π is 6 and the two digits starting at the 27th digit are 27.

Your task is to write a program that finds self-locating strings in π (A064810). 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