0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CodeIQ:「デジタルタブー」問題

Last updated at Posted at 2025-05-02

Kawazoe(@riverplus)さんのCodeIQの問題です。問題と解説がこちらにあります。
->http://paranoiactoadstool.blog.fc2.com/blog-entry-437.html
自然数を0,3,6,9を含まない数字で表すと10^7はどうあらわされるかという問題です。
「妙な(?)数列の一般項」と同じ問題です。
テストケースは裏 RjpWikiさんに御世話になりましたがリンク切れてしまいました。
場所が変わっていたけれど見つかりました。(2025.5.3追加)
->https://sangaku0418.hatenablog.com/entry/2014/11/26/000903

6進数で作っておいて後で変換するのがが簡単です。
0がなくその代わりに6がありますので、例えば6進数の2000は1556と表されます。
そこで桁上がりの調節に毎桁ごとに1を引いて割り剰余に1を足しました。
(表の横縦に1から始まる番号をつけるのと同じです)
そのあとリストの値と変換します。

#julia ver 1.41,1.53,1.10.3

function solve(n,a)
    x,y,k = n,0,0
    ar=[1,2,4,5,7,8]
    while x > 0
        x,r = divrem(x-1,6)
        r = ar[r+1]
        y += r * 10^k
        k +=1
    end
    s = a == y ? "ok " : "no "
    println(s,y)
end

solve(30,58)
solve(10^7, 775178155)
solve(10^6,44244455)
solve(10^5, 1858755)
solve(10^4, 115155)
solve(10^3, 5455)
solve(10^2, 255)
solve(3333, 24244)
%SWI-Prolog ver 7.42,7.64,9.25
%:-initialization(start).   %ideone,Online Prolog Compiler
%start.

change(X,R):-L=[1,2,4,5,7,8],nth1(X,L,R).

lostx(_,_,_,0,R,R):-!.
lostx(B,L,N,X,Y,R):-
   X1 is X-1,divmod(X1,B,D,M),M1 is M+1,nth1(M1,L,M2),
   Y1 is Y+M2*10^N,N1 is N+1,lostx(B,L,N1,D,Y1,R).

solve(N,R):-L=[1,2,4,5,7,8],lostx(6,L,0,N,0,R).

start:-f(N,A),(solve(N,R)->(R==A->Str=" ok  ";Str=" no  ");
    write(fail)),write(" "),write(Str),writeln(N->R),fail.
start.

f(30,58).
f(10^7, 775178155).
f(10^6,44244455).
f(10^5, 1858755).
f(10^4, 115155).
f(10^3, 5455).
f(10^2, 255).
f(3333, 24244).
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?