Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
今日の"どう書く"問題のアルゴリズム例
Search
Takashi Kanemoto
August 28, 2019
Programming
1.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
今日の"どう書く"問題のアルゴリズム例
Nagoya.php #17 補足資料
https://nagoyaphp.connpass.com/event/139314/
Takashi Kanemoto
August 28, 2019
More Decks by Takashi Kanemoto
See All by Takashi Kanemoto
API Platformを活用したPHPによる本格的なWeb API開発 / api-platform-book-intro
ttskch
2
250
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
5
1.5k
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
570
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
1.3k
今年書いた技術記事で伸びたやつの自慢と自分の中では力作なのにさっぱり伸びなかったやつの供養 / My Tech Articles 2024
ttskch
2
210
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
48
44k
データベース/SQL超入門!完全初心者向けに世界一分かりやすく解説します
ttskch
2
8.3k
Symfony UX Autocompleteとかいう 顧客が本当に必要だったもの
ttskch
0
2.3k
就活生あるいは新人エンジニアさんへのお節介なアドバイス
ttskch
0
1.7k
Other Decks in Programming
See All in Programming
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
190
The NotImplementedError Problem in Ruby
koic
1
1.1k
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
240
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.9k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.6k
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.7k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
160
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
130
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
140
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
100
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.6k
Featured
See All Featured
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
220
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Building AI with AI
inesmontani
PRO
1
1.1k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
550
Between Models and Reality
mayunak
4
360
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
180
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Invisible Side of Design
smashingmag
301
52k
Optimizing for Happiness
mojombo
378
71k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Transcript
ࠓͷ”Ͳ͏ॻ͘”ͷ ΞϧΰϦζϜྫ 2019/08/28 #nagoyaphp @ttskch
ʹ͍ͭͯ
͖ͨͭͪʢ໊͋ͩʣ • (ג)ΧϧςοτίϛϡχέʔγϣϯζCTO • WebΤϯδχΞྺ7ɺPHPྺ7 • #nagoyaphpʊ • #symfony_meetup
͖ͨͭͪʢ໊͋ͩʣ • ͓ख͍ͨ͠ຊʢ2015/12ʣ • Schooߨࢣʢ2018/04ʣ ʮGitHubΛͬͨσϓϩΠࣗಈԽ࣮ફʯ ʮجຊ͔Β͔ͬ͠ΓֶͿ Symfony2ೖʯ
ͨ ͭ ͖ ͪ twitter.com/ttskch ͋͞ɺ͋ͳͨࠓ͙͢ϑΥϩʔ͠Α͏!!! શແྉ ·͞ʹ໊ݴػ ਓੜͰେͳ͜ͱɺͯ͢ ͖͔ͨͭͪΒڭΘͬͨ
͖ͨͭͪΛϑΥϩʔͨ͠Β ࠊ௧͕࣏Γ·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋ 100% ͍ͯ͠Δ ͍ͯ͠ͳ͍ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ
ձࣾʹ͍ͭͯ
(ג)Χϧςοτίϛϡχέʔγϣϯζ https://quartet-communications.com
(ג)Χϧςοτίϛϡχέʔγϣϯζ https://www.google.co.jp/search?q=Χʔςϯ
(ג)Χϧςοτίϛϡχέʔγϣϯζ https://lisket.jp
We are hiring!ʢϚδͰʣ • PHPer/JSerઈࢍେืूதʂʢϦϞʔτόΠτՄʣ • Symfony+AngularͰɺWebࠂۀքΛ ϗϫΠτʹ͢ΔΈΛ࡞ͬͯ·͢ • ܦӦϏδϣϯʮੈքҰޮతͳཧళʯ
We are hiring!ʢϚδͰʣ • ࣗࣾαʔϏεʮLisketʯͰࣗࣾͷۀޮԽ • શࣾһͷฏۉۀ͕࣌ؒ5h/݄͙Β͍Ͱ͢ ಛผड
We are hiring!ʢϚδͰʣ ΤϯδχΞͷಇ͖͢͞େࣄʹͯ͠·͢✨
We are hiring!ʢϚδͰʣ ·͓͚ͣͩͰʂؾܰʹ͔͚͍ͯͩ͘͞
/30 We are hiring!ʢϚδͰʣ ࠾༻αΠτ͋Γ·͢ʂͥͻ͍ͯΈ͍ͯͩ͘͞ 14 https://quartetcom.co.jp/recruit/engineer/
ࠓͷ”Ͳ͏ॻ͘”ͷ ΞϧΰϦζϜྫ 2019/08/28 #nagoyaphp @ttskch
http://nabetani.sakura.ne.jp/hena/orde16nontri/
ී௨ʹΔͱ
public function run(string $input) : string { $dec = intval($input);
while (true) { $bin = decbin(strval(++$dec)); if (strpos($bin, '000') === false && strpos($bin, '111') === false) { return strval($dec); } } } 1૿͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈͔ʁʯΛνΣοΫ͢Δ
public function run(string $input) : string { $dec = intval($input);
while (true) { $bin = decbin(strval(++$dec)); if (strpos($bin, '000') === false && strpos($bin, '111') === false) { return strval($dec); } } } 1૿͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈͔ʁʯΛνΣοΫ͢Δ ೖྗͱظͷࠩʹൺྫͯ͠ ܭࢉྔ͕૿͑Δ
public function run(string $input) : string { $dec = intval($input);
while (true) { $bin = decbin(strval(++$dec)); if (strpos($bin, '000') === false && strpos($bin, '111') === false) { return strval($dec); } } } 1૿͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈͔ʁʯΛνΣοΫ͢Δ ೖྗͱظͷࠩʹൺྫͯ͠ ܭࢉྔ͕૿͑Δ 100000000000000 (2) ͱ͔Λೖྗ͞ΕΔͱϠόΠ
ͯ͠ΈΔ
ྫᶃ 22
ྫᶃ 23 100010110001
24 100010110001 ࠷ॳͷ 000 Λݟ͚ͭΔ ྫᶃ
25 100110110001 ͦΕΛ 001 ʹஔ͖͑Δ ྫᶃ
26 100110110001 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶃ
27 100100100100 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶃ
28 100100100100 ྫᶃ
ྫᶄ 29
ྫᶄ 30 101110101101
31 101110101101 ࠷ॳͷ 0111 Λݟ͚ͭΔ ྫᶄ
32 110010101101 ͦΕΛ 1001 ʹஔ͖͑Δ ྫᶄ
33 110010101101 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶄ
34 110010010010 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶄ
35 110010010010 ྫᶄ
·ͱΊΔͱ 36
37 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ
38 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ
39 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ઌ಄͕111ͩͬͨΒʁ
40 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ
41 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
42 000͕ઌʹ ͋Δ߹ ࠷ॳͷ000Λ001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
͞Βʹͻͱ…
43 1000͕ઌʹ ͋Δ߹ ࠷ॳͷ1000Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
44 1000͕ઌʹ ͋Δ߹ ࠷ॳͷ1000Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
45 1000͕ઌʹ ͋Δ߹ ࠷ॳͷ1000Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ࠷ॳͷ0111Λ1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
46 1000͕ઌʹ ͋Δ߹ ࠷ॳͷ1000·ͨ0111Λ 1001ʹஔͯ͠ ΓΛ001ͷ॥ͰຒΊΔ 0111͕ઌʹ ͋Δ߹ ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ
ྫᶅ 47
ྫᶅ 48 110111010110
ྫᶅ 49 01101110101100 ઌ಄ʹ 0 Λ༩͢Δ
50 01101110101100 ࠷ॳͷ 0111 Λݟ͚ͭΔ ྫᶅ
51 01110010101100 ͦΕΛ 1001 ʹஔ͖͑Δ ྫᶅ
52 01110010101100 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶅ
53 01110010010010 ଓ͖Λͯ͢ 001001... Ͱஔ͖͑Δ ྫᶅ
54 111001001001 ʁ ྫᶅ
55 111001001001 ൱ʂ ྫᶅ
མͱ݀͠ 56 • 0111Λ1001ʹஔͨ͜͠ͱͰɺ 111͕৽ͨʹ࡞ΒΕͯ͠·͏߹͕͋Δ • ಉ͡ૢ࡞Λ܁Γฦ͢ඞཁ͕͋Δʂ
݁ 57
58 • ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ • ࠷ॳͷ1000·ͨ0111Λ1001ʹஔͯ͠ • ΓΛ001ͷ॥ͰຒΊΔ • ઌ಄ͷ0ΛऔΓআ͘ •
…ͱ͍͏ૢ࡞Λඇࡾ࿈ʹͳΔ·Ͱ܁Γฦ͢
59 • ͋Β͔͡Ίઌ಄ʹ0Λ༩্͓͍ͯͨ͠Ͱ • ࠷ॳͷ1000·ͨ0111Λ1001ʹஔͯ͠ • ΓΛ001ͷ॥ͰຒΊΔ • ઌ಄ͷ0ΛऔΓআ͘ •
…ͱ͍͏ૢ࡞Λඇࡾ࿈ʹͳΔ·Ͱ܁Γฦ͢
࣮ྫ
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } }
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } Ұ࿈ͷॲཧΛ܁Γฦ͢
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } 2ਐʹมͯ͠ઌ಄ʹ0Λ༩
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } ࠷ॳͷ1000·ͨ0111Λ୳͢
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } ݟ͔ͭΒͳ͚Εඇࡾ࿈ͷ
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } ઌ಄ͷ0ΛऔΓআ͖ͭͭ (1000|0111)ΑΓલͷ෦Λୀආ
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } (1000|0111)ΑΓޙͷ෦Λ001...Ͱஔ
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } } ʮલʴ1001ʴޙʯͱ࿈݁ͯ͠10ਐʹ
public function run(string $input) : string { $dec = intval($input)
+ 1; while (true) { $bin = '0' . decbin($dec); preg_match('/(.*?)(1000|0111)(.*)/', $bin, $m); if (empty($m)) { return strval($dec); } $head = ltrim($m[1], '0'); $tailLength = strlen($m[3]); $tail = substr( str_repeat('001', intval(floor($tailLength / 3)) + 1), 0, $tailLength); $dec = strval(bindec($head . '1001' . $tail)); } }
https://github.com/ ttskch/nagoyaphp17
@ttskch ʘThanks!ʗ