Skip to content

Commit

Permalink
Integer#congruum?: Add
Browse files Browse the repository at this point in the history
  • Loading branch information
runpaint committed Jan 14, 2010
1 parent 1ffe8c6 commit 44b122c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
35 changes: 18 additions & 17 deletions lib/numb.rb
@@ -1,23 +1,24 @@
# coding: utf-8

libs = %w{abundancy abundant achilles almost_perfect amicable aspiring
automorphic balanced_prime carmichael carol composite coprime
core cototient cube d decagonal deficient dodecagonal dihedral_prime
dudeney economical emrip equidigital extravagant factorion fibonacci
friendly frugal happy harshad heptagonal hexagonal highly_composite
highly_abundant hilbert hyperperfect idoneal impolite integer_p
interprime jacobsthal_lucas kaprekar keith knodel k_perfect kynea
leonardo leyland lucas lucas_carmichael mersenne_prime minimal
mms_pair mobius myriagonal narcissistic next_prev_prime n_gonal
nivenmorphic noncototient nth_prime number_of_divisors octagonal
ordinal ore parasitic pentagonal perfect perfect_power polite
polydivisible powerful practical prime_count prime_signature
primitive_pseudoperfect primorial pronic proth refactorable repunit
rhonda rough self self_descriptive semiperfect semiprime
smarandache_wellin smith smooth sophie_germain_prime sphenic square
square_free sublime sum_of_squares superabundant superperfect totient
triangular trimorphic undulating unitary_perfect unitary_divisor
untouchable vampire weird zeisel
automorphic balanced_prime carmichael carol congruum composite
coprime core cototient cube d decagonal deficient dodecagonal
dihedral_prime dudeney economical emrip equidigital extravagant
factorion fibonacci friendly frugal happy harshad heptagonal
hexagonal highly_composite highly_abundant hilbert hyperperfect
idoneal impolite integer_p interprime jacobsthal_lucas kaprekar
keith knodel k_perfect kynea leonardo leyland lucas lucas_carmichael
mersenne_prime minimal mms_pair mobius myriagonal narcissistic
next_prev_prime n_gonal nivenmorphic noncototient nth_prime
number_of_divisors octagonal ordinal ore parasitic pentagonal
perfect perfect_power polite polydivisible powerful practical
prime_count prime_signature primitive_pseudoperfect primorial pronic
proth refactorable repunit rhonda rough self self_descriptive
semiperfect semiprime smarandache_wellin smith smooth
sophie_germain_prime sphenic square square_free sublime
sum_of_squares superabundant superperfect totient triangular
trimorphic undulating unitary_perfect unitary_divisor untouchable
vampire weird zeisel
}

class Integer
Expand Down
13 changes: 13 additions & 0 deletions lib/numb/congruum.rb
@@ -0,0 +1,13 @@
class Integer
def congruum?
# Fibonacci proved that h|24
# Fermat’s right triangle theorem shows h is not square
return false unless divides?(24) and not square?
h = self
(1..Math.sqrt(h)).any? do |n|
(n.succ..Math.sqrt(h)).any? do |m|
h == (4 * m * n) * (m**2 - n**2)
end
end
end
end
20 changes: 20 additions & 0 deletions spec/congruum_spec.rb
@@ -0,0 +1,20 @@
describe Integer, "#congruum?" do
# A057102
@seq = [24,96,120,240,336,384,480,720,840,960,1320,1344,
1536,1920,1944,2016,2184,2520,2880,3360,3696,3840,
3960,4896,5280,5376,5544,6144,6240,6840,6864,7680,
7776,8064,8736,9240,9360,9720,10080,10296,10920,
11520,12144].to_seq

@seq.each do |n|
it "should return true for congruum #{n}" do
n.should be_congruum
end
end

@seq.invert.sample(10).each do |n|
it "should return false for non-congruum #{n}" do
n.should_not be_congruum
end
end
end

0 comments on commit 44b122c

Please sign in to comment.