Upgrade to Pro — share decks privately, control downloads, hide ads and more …

【2018/07/29 TokyuRuby会議12】Project Eulerから生まれたワンライナーたち

【2018/07/29 TokyuRuby会議12】Project Eulerから生まれたワンライナーたち

2018/07/29にあったTokyuRuby会議12での発表スライドです
終盤の 'require不要のワンライナー' についての解説:http://expajp-tech.hatenablog.com/entry/2018/07/31/232737

スライドの23枚目、== 4となっている箇所は ==5の誤りです
申し訳ありません

Shu OGAWARA

July 29, 2018
Tweet

More Decks by Shu OGAWARA

Other Decks in Technology

Transcript

  1. Project Euler͔Βੜ·Εͨ
    ϫϯϥΠφʔͨͪ
    Shu OGAWARA (@expajp)
    TokyuRubyձٞ12
    2018/07/29 Sun.

    View full-size slide

  2. ࣗݾ঺հ
    • Shu OGAWARA (@expajp)
    – Ruby/Railsྺ 1೥΄Ͳ
    – ग़਎ɿௗऔɺֶੜ࣌୅ɿਆށ
    – ϦϯΧʔζ
    – झຯͰ߹এஂʹॴଐ
    1

    View full-size slide

  3. Project Eulerͬͯ஌ͬͯ·͔͢ʁ
    2

    View full-size slide

  4. ϨΦϯϋϧτɾΦΠϥʔ (1707-1783)
    • εΠεͷ਺ֶऀ
    • ࿦จࣥච଎౓͸800ϖʔδ/೥
    • ۀ੷͕๲େ͗ͯ͢
    100೥ܦͬͯ΋શू͕ະ׬
    3

    View full-size slide

  5. Project Euler
    • ίϯϐϡʔλͰղ͘਺ֶͷ໰୊ू
    – ϓϩίϯతͳ໰୊
    – ͚ͩͲͬ͘͡Γղ͚Δ
    5

    View full-size slide

  6. Project Euler
    • RubyͰͷ௅ઓऀ͸൪໨ʹଟ͍
    – Python, C/C++, Java, C#, Haskellʹଓ͍ͯRuby
    – ϓϩϑΟʔϧʹݴޠΛॻ͍͍ͯΔਓͷ౷ܭ
    6

    View full-size slide

  7. 7
    ൃදऀ͸234Ґʢ95໰ղ౴ʣ

    View full-size slide

  8. 8
    ൃදऀ͸234Ґʢ95໰ղ౴ʣ

    View full-size slide

  9. ϫϯϥΠφʔ
    • 1ߦͰॻ͘ϓϩάϥϜͷ͜ͱ
    – ʢࣗ෼ͷதͰ͸ʣϥΠϒϥϦಡࠐ͸ؚ·ͳ͍
    • Rubyͷ୉ޣຯ
    • ͨͱ͑͹FizzBuzz
    9

    View full-size slide

  10. ͜ͷൃදͷझࢫ
    • Project EulerΛ100໰ۙ͘·Ͱղ͍͖ͯͨͷ
    ͰɺϫϯϥΠφʔͰղ͚Δ໰୊ΛνϣΠε
    ΈΜͳʹղ͍ͯ΋Β͏
    – ղ౴ྫΛ঺հ
    10

    View full-size slide

  11. ͜ͷൃදͷझࢫ
    • Project EulerΛ100໰ۙ͘·Ͱղ͍͖ͯͨͷ
    ͰɺϫϯϥΠφʔͰղ͚Δ໰୊ΛνϣΠε
    ΈΜͳʹղ͍ͯ΋Β͏
    – ղ౴ྫΛ঺հ
    11
    – ΈΜͳʹղ͍ͯ΋Β͏

    View full-size slide

  12. ਲͬ෷͏! લʹΞλϚ࢖͍·͠ΐ͏ʂ
    12

    View full-size slide

  13. Please Get Ready
    13
    $ irb
    OR
    $ pry
    $ rib
    OR

    View full-size slide

  14. Are You Ready ?
    14

    View full-size slide

  15. ୈ̍໰ (30sec. Hint: 15sec.)
    1, 2, …, 10 ʹ͍ͭͯ,
    ೋ৐ͷ࿨͸, 12 + 22 + ... + 102 = 385
    ࿨ͷೋ৐͸, (1 + 2 + ... + 10)2 = 3025
    ࠩ͸ 3025 - 385 = 2640 ͱͳΔ.
    ಉ༷ʹͯ͠, 1, 2, …, 100ʹ͍ͭͯʮೋ৐ͷ࿨ʯͱʮ࿨ͷೋ৐ʯ
    ͷࠩΛٻΊΑ.
    ग़యɿProject Euler – Problem 6
    15

    View full-size slide

  16. ୈ̍໰ (30sec. Hint: 15sec.)
    1, 2, …, 10 ʹ͍ͭͯ,
    ೋ৐ͷ࿨͸, 12 + 22 + ... + 102 = 385
    ࿨ͷೋ৐͸, (1 + 2 + ... + 10)2 = 3025
    ࠩ͸ 3025 - 385 = 2640 ͱͳΔ.
    ಉ༷ʹͯ͠, 1, 2, …, 100ʹ͍ͭͯʮೋ৐ͷ࿨ʯͱʮ࿨ͷೋ৐ʯ
    ͷࠩΛٻΊΑ.
    ग़యɿProject Euler – Problem 6
    16
    ώϯτɿRuby 2.4Ҏ߱ͳΒ Array#sum ͕࢖͑·͢

    View full-size slide

  17. ୈ̎໰ (1min. Hint: 30sec.)
    20ੈلʢ1901೥1݄1೔͔Β2000೥12݄31೔ʣதʹ
    ݄ͷॳΊ͕೔༵೔ʹͳΔͷ͸Կճ͋Δ͔
    ͳ͓, ೥1݄1೔͸݄༵೔Ͱ͋Γ,
    20ੈلʹ͓͍ͯ4ͷഒ਺ͷ೥͸ඞͣ͏Δ͏೥Ͱ͋Δ.
    ग़య: Project Euler - Problem 19
    18

    View full-size slide

  18. ୈ̎໰ (1min. Hint: 30sec.)
    20ੈلʢ1901೥1݄1೔͔Β2000೥12݄31೔ʣதʹ
    ݄ͷॳΊ͕೔༵೔ʹͳΔͷ͸Կճ͋Δ͔
    ͳ͓, ೥1݄1೔͸݄༵೔Ͱ͋Γ,
    20ੈلʹ͓͍ͯ4ͷഒ਺ͷ೥͸ඞͣ͏Δ͏೥Ͱ͋Δ.
    ग़య: Project Euler - Problem 19
    19
    ώϯτɿϥΠϒϥϦಡࠐ͸ϫϯϥΠφʔʹؚΈ·ͤΜ

    View full-size slide

  19. ղ౴ྫ
    20
    DateϥΠϒϥϦͷಡࠐ͕ඞཁ

    View full-size slide

  20. ࣮͸ɺϥΠϒϥϦͳ͠Ͱ΍Δํ๏΋͋Δ
    21

    View full-size slide

  21. ղઆ͸ͦͷ͏ͪϒϩάʹͰ΋ॻ͖·͢!
    23

    View full-size slide

  22. ·ͱΊ
    • 1SPKFDU &VMFS͸ͨͷ͍͠
    • ϫϯϥΠφʔͰղ͘ͱ΋ͬͱͨͷ͍͠
    • ࢓ࣄͷ߹ؒʹΞλϚͷମૢ͸͍͔͕Ͱ͔͢
    24

    View full-size slide

  23. Thank you for Listening!!
    25
    Ruby Logo(https://www.ruby-lang.org/ja/ ) CC BY-NC-SA 2.5

    View full-size slide