Skip to content

Instantly share code, notes, and snippets.

@horothesun
Created October 9, 2022 20:09
Show Gist options
  • Save horothesun/7e91e5992355688ce121f14960db6de2 to your computer and use it in GitHub Desktop.
Save horothesun/7e91e5992355688ce121f14960db6de2 to your computer and use it in GitHub Desktop.
Find a number X such that "prepending" 4 to X equals to 4 times the number generated by "suffixing" X with 4
%% Find a number X such that "prepending" 4 to X equals to
%% 4 times the number generated by "suffixing" X with 4.
%%
%% X = X1,...,Xn . 4,X1,...,Xn = 4 * X1,...Xn,4
%%
%% :- find( 4, 9000, 11000, N ).
%%
natural( 0 ).
natural( s(X) ) :- natural( X ).
leq( 0, X ) :- natural( X ).
leq( s(X), s(Y) ) :- leq( X, Y ).
numbToPeano( 0, 0 ).
numbToPeano( N, s(X) ) :-
N > 0,
M is N - 1,
numbToPeano( M, X ).
peanoToNumb( 0, 0 ).
peanoToNumb( s(X), N ) :-
peanoToNumb( X, M ),
N is M + 1.
digits( X, 1 ) :- X =< 9.
digits( X, D ) :-
X > 9,
X1 is X // 10,
digits( X1, D1 ),
D is D1 + 1.
generateNumber( InfLimit, SupLimit, N ) :-
numbToPeano( SupLimit, SupLimitPeano ),
numbToPeano( InfLimit, InfLimitPeano ),
leq( Peano, SupLimitPeano ),
leq( InfLimitPeano, Peano ),
peanoToNumb( Peano, N ).
find( C, InfLimit, SupLimit, N ) :-
generateNumber( InfLimit, SupLimit, N ),
digits( N, D ),
CN is N + C * 10**D,
NC is 10 * N + C,
CN =:= C * NC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment