Sultan's daughter
A princess answers a string of digits with another string of digits. Find one she answers with itself.
Raymond Smullyan, 1997 · Turing complete: no · ●●○ moderate · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
Raymond Smullyan defined this function in his 1997 book The Riddle of Scheherazade. In the story the sultan's daughter computes it, and prince Al-Khizr has to find strings with special properties to show he is worthy of her. One of them is a fixed point, a string the princess gives back unchanged.
Read the story
A 1 and a 2 around a string quote it. The digits 3 to 7 in front of the quote work on it, the one nearest the quote first. Every other string has no answer at all.
The machine
| Digits | Answer |
|---|---|
1x2 | x itself |
3x | The answer to x, twice |
4x | The answer to x, reversed |
5x | The answer to x without its first digit |
6x | 1, then the answer to x |
7x | 2, then the answer to x |
x is any string of digits that is not empty. A 5 needs an answer of at least two digits.
Example
The second is a fixed point.
The second is a fixed point. Its quote holds 47431. The 3 doubles that to 4743147431, the 4 reverses it to 1347413474, the 7 puts a 2 in front, and the last 4 reverses it all into 47431474312, the program itself. The third is the fixed point from the book, 14 digits long.
Patterns it teaches
- String rewriting: five rules on strings of digits.
- Fixed points: a string the function gives back unchanged, close kin to a quine.
Further
- esolangs.org/wiki/Sultan's_daughter
- McCulloch's second and third machines, from an earlier book by Smullyan, are related functions.
See also
String-rewriting paradigm
Write your own in Run Convert a program from Sultan's daughter
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: sultan-s-daughter.
Command line
shib run sultan-s-daughter -e '47431474312'shib manual sultan-s-daughterSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/sultan-s-daughter.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org