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

Car Crash Photos

November 22, 2018

Today is the Thanksgiving holiday in the United States, and I have much to be thankful for this year, being alive and mostly uninjured after a horrible car crash. While I was in the intensive care unit, I lost over a gallon of blood where the steering wheel punched me in the belly. That blood now sits in my belly, forming a huge bruise and causing me to suffer the fatigue and occasional dizzyness of anemia. I am improving slowly, and will return to work half-days starting next week.

The pictures below were provided by my insurance company. As you look at them remember that I walked away from the crash.

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

CarCrash01

Car Crash

October 25, 2018

On Saturday morning I had a head-on collision at 75MPH, Medevac helicopter, serious internal bleeding, no broken bones. I spent four days in hospital, including one-and-a-half days in intensive care. I am home now, moving slowly but mostly okay.

I was alone in the car on an interstate highway early in the morning, heading to a weekend with friends away from home. About a minute ahead of me, an 18-wheel tractor/trailer rig hit a deer, lost control, jack-knifed and overturned, leaving the right lane blocked with the bottom of the tractor facing oncoming traffic. Because it was still astronomical twilight, with no moon and no street lamps, I did not see the tractor until it was within range of my headlights, and I was unable to avoid the collision, although I did partially swerve and struck only the right half of my car, from centerline to right side, spinning twice.

In the car about fifteen seconds behind me were four volunteer firefighters on their way to start their shift, so they were on the scene in seconds. They helped me out of the car, and I walked under my own power to the ambulance. The trainee driver of the truck was uninjured. The lead driver of the truck was asleep in the cab’s bed, and suffered a broken left leg.

My left ankle has a deep bruise, but is not broken, from striking the brake pedal; it is painful. My belly has a deep bruise from striking the steering wheel, and was the source of the internal bleeding; I take blood-thinning medication due to two prior pulmonary embolisms, so the bleeding was serious. I was flown from a local hospital to a regional first-class trauma center shortly after the crash. They monitored the bleeding, which stopped on its own after about two days without need for surgery (which was a real possibility). The bruise on my right belly is about twice the size of my right hand with fingers splayed wide, and is mostly black, with a few tinges of deep purple; it is not painful unless I bend or twist badly, which I have learned not to do. I am wearing a corset (the hospital calls it an abdominal binder) and taking a very low dose of pain-killers.

I’ve spoken to my boss; she even came to the hospital to visit and bring flowers. I’ll be away from work for at least several weeks, and away from blogging for just as long. I would appreciate it if my readers keep doing exercises while I am away; just look for any old exercises that strike your fancy.

Square Digit Chains

October 19, 2018

Today’s task appears on Project Euler, Rosetta Code, and Sloane’s, so it is well worth a look; we even did a version of this task in a previous exercise. Here is the Project Euler version of the problem:

A number chain is created by continuously added the square of the digits of a number to form a new number until it has been seen before. For example:

44 → 32 → 13 → 10 → 1 → 1

85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.

How many starting numbers below ten million will arrive at 89?

Your task is to write a program to find the count of starting numbers below ten million that arrive at 89, following the usual Project Euler rule that your computation may take no more than a minute of computation on a recent-vintage personal computer. 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

Five Weekends

October 16, 2018

This task comes to us from Rosetta Code: The month of October 2010 has five Fridays, five Saturdays and five Sundays.

Your task is to count and make a list of all months from 1900 to 2100, inclusive, that have five Fridays, Saturdays and Sundays, and to count and make a list of all years from 1900 to 2100, inclusive, that have no such months. 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

Boxing The Compass

October 12, 2018

Modern navigators use the 360° angles of a circle to point to compass directions, but traditional mariners use compass points with names like SW and ENE.

Your task is to write a program that converts from degrees to compass points. 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

Babbage’s Number

October 9, 2018

Charles Babbage, whose Analytical Engine was a direct predecessor of today’s digital computer, gave this example of a problem that his Analytical Engine could solve in an 1837 letter to Lord Bowden:

What is the smallest positive integer whose square ends in the digits 269,696?

Babbage knew that 99,736 has a square with the required ending, but didn’t know if there was a smaller number.

Your task is to find Babbages’s number. 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