This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| class Prime | |
| def twins n = nil | |
| return to_enum :twins, n unless block_given? | |
| each(n).each_cons(2) do |p1, p2| | |
| yield [p1, p2] if p2 - p1 == 2 | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| class Prime | |
| def cosins n = nil | |
| return to_enum :cosins, n unless block_given? | |
| yield [3, 7] | |
| each(n).each_cons(2) do |p1, p2| | |
| yield [p1, p2] if p2 - p1 == 4 | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # original: http://log.ttsky.net/article/19515687.html | |
| require 'prime' | |
| class Prime | |
| # An implementation of +Prime::PseudoPrimeGenerator+. | |
| class A2Generator < PseudoPrimeGenerator | |
| def initialize | |
| @h = {} | |
| @n = nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # original: Prime::EratosthenesGenerator and Prime::EratosthenesSieve | |
| require 'prime' | |
| require 'singleton' | |
| class Prime | |
| # An implementation of +Prime::PseudoPrimeGenerator+. | |
| # | |
| # Uses +A2ESieve+. | |
| class A2EGenerator < PseudoPrimeGenerator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # morphological.rb | |
| # original: morphological.py (http://gihyo.jp/dev/serial/01/machine-learning/0003) | |
| require 'MeCab' | |
| module Morphological | |
| def split sentence | |
| mecab = MeCab::Tagger.new | |
| node = mecab.parseToNode sentence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sq? n | |
| # ↓32bit整数の範囲ならとりあえずこれでOK | |
| Math.sqrt(n).round**2 == n | |
| end | |
| def project_euler_45 n=143 | |
| loop do | |
| n += 1 | |
| h = n * (2 * n - 1) | |
| next unless sq?(8 * h + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| def project_euler_46 | |
| Prime.each_cons(2) do |p1, p2| | |
| (p1 + 2).step(p2 - 2, 2) do |n| | |
| return n unless (1..Math.sqrt((n-3)/2)).find{|m|(q = n - 2*m*m).prime?} | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| def project_euler_47 n=4 | |
| return if n < 2 | |
| Prime.each_cons(2) do |p1, p2| | |
| nums = ((p1+1)...p2).select{|m|m.prime_division.size==n} | |
| idx = 0.upto(nums.size-n).find{|i|nums[i+n-1]==nums[i]+n-1} | |
| return nums[idx] if idx | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def project_euler_68 | |
| res = 0 | |
| arr = (1..9).to_a | |
| arr.combination(5) do |(*inner)| | |
| s = inner.inject(&:+) | |
| next unless s % 5 == 0 | |
| t = s.div(5) + 11 | |
| outer = (arr - inner) << 10 | |
| o_min = outer.min | |
| inner.combination(2) do |a, b| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'prime' | |
| class Integer | |
| def phi | |
| self.prime_division.inject(1){|m,(p,k)|m*=p**k-p**(k-1)} | |
| end | |
| def phi_ratio | |
| self.to_f / self.phi | |
| end | |
| end |