esolang

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

Expected: 2323

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

DigitsAnswer
1x2x itself
3xThe answer to x, twice
4xThe answer to x, reversed
5xThe answer to x without its first digit
6x1, then the answer to x
7x2, 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.
Expected: 47431474312
Expected: 47536414753642

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

Further

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-daughter

Source

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