Skip to content

Commit 5563e47

Browse files
committed
2 parents 1589999 + d10bac9 commit 5563e47

13 files changed

Lines changed: 639 additions & 28 deletions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Beginning Ruby - Second Edition
2+
# Exercise from Chapter 4 - Developing your first Ruby Application
3+
# Sam Gerber
4+
5+
# Required basic features:
6+
# * character count
7+
# * character count (excluding spaces)
8+
# * line count
9+
# * word count
10+
# * sentence count
11+
# * paragraph count
12+
# * average number of words per sentence
13+
# * average number of sentences in a paragraph
14+
15+
lines = File.readlines("lib/text.txt")
16+
line_count = lines.size
17+
text = lines.join
18+
19+
total_characters = text.length
20+
21+
total_characters_nospaces = text.gsub(/\s+/, '').length
22+
23+
word_count = text.split.length
24+
25+
sentence_count = text.split(/\.|\?|!/).length
26+
27+
paragraph_count = text.split(/\n\n/).length
28+
29+
average_words_per_sentence = word_count / sentence_count
30+
31+
average_sentences_per_paragraph = sentence_count / paragraph_count
32+
33+
puts "#{line_count} lines"
34+
puts "#{total_characters} characters"
35+
puts "#{total_characters_nospaces} characters (excluding spaces)"
36+
puts "#{word_count} words"
37+
puts "#{sentence_count} sentences"
38+
puts "#{paragraph_count} paragraphs"
39+
puts "#{average_words_per_sentence} words per sentence (on average)"
40+
puts "#{average_sentences_per_paragraph} sentences per paragraph (on average)"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Among other public buildings in a certain town, which for many
2+
reasons it will be prudent to refrain from mentioning, and to
3+
which I will assign no fictitious name, there is one anciently
4+
common to most towns, great or small: to wit, a workhouse; and
5+
in this workhouse was born; on a day and date which I need not
6+
trouble myself to repeat, inasmuch as it can be of no possible
7+
consequence to the reader, in this stage of the business at all
8+
events; the item of mortality whose name is prefixed to the head
9+
of this chapter.
10+
11+
For a long time after it was ushered into this world of sorrow
12+
and trouble, by the parish surgeon, it remained a matter of
13+
considerable doubt whether the child would survive to bear any
14+
name at all; in which case it is somewhat more than probable that
15+
these memoirs would never have appeared; or, if they had, that
16+
being comprised within a couple of pages, they would have
17+
possessed the inestimable merit of being the most concise and
18+
faithful specimen of biography, extant in the literature of any
19+
age or country.
20+
21+
Although I am not disposed to maintain that the being born in a
22+
workhouse, is in itself the most fortunate and enviable
23+
circumstance that can possibly befall a human being, I do mean to
24+
say that in this particular instance, it was the best thing for
25+
Oliver Twist that could by possibility have occurred. The fact
26+
is, that there was considerable difficulty in inducing Oliver to
27+
take upon himself the office of respiration,--a troublesome
28+
practice, but one which custom has rendered necessary to our easy
29+
existence; and for some time he lay gasping on a little flock
30+
mattress, rather unequally poised between this world and the
31+
next: the balance being decidedly in favour of the latter. Now,
32+
if, during this brief period, Oliver had been surrounded by
33+
careful grandmothers, anxious aunts, experienced nurses, and
34+
doctors of profound wisdom, he would most inevitably and
35+
indubitably have been killed in no time. There being nobody by,
36+
however, but a pauper old woman, who was rendered rather misty by
37+
an unwonted allowance of beer; and a parish surgeon who did such
38+
matters by contract; Oliver and Nature fought out the point
39+
between them. The result was, that, after a few struggles,
40+
Oliver breathed, sneezed, and proceeded to advertise to the
41+
inmates of the workhouse the fact of a new burden having been
42+
imposed upon the parish, by setting up as loud a cry as could
43+
reasonably have been expected from a male infant who had not been
44+
possessed of that very useful appendage, a voice, for a much
45+
longer space of time than three minutes and a quarter.
46+
47+
As Oliver gave this first proof of the free and proper action of
48+
his lungs, the patchwork coverlet which was carelessly flung over
49+
the iron bedstead, rustled; the pale face of a young woman was
50+
raised feebly from the pillow; and a faint voice imperfectly
51+
articulated the words, 'Let me see the child, and die.'
52+
53+
The surgeon had been sitting with his face turned towards the
54+
fire: giving the palms of his hands a warm and a rub
55+
alternately. As the young woman spoke, he rose, and advancing to
56+
the bed's head, said, with more kindness than might have been
57+
expected of him:
58+
59+
'Oh, you must not talk about dying yet.'
60+
61+
'Lor bless her dear heart, no!' interposed the nurse, hastily
62+
depositing in her pocket a green glass bottle, the contents of
63+
which she had been tasting in a corner with evident satisfaction.
64+
65+
'Lor bless her dear heart, when she has lived as long as I have,
66+
sir, and had thirteen children of her own, and all on 'em dead
67+
except two, and them in the wurkus with me, she'll know better
68+
than to take on in that way, bless her dear heart! Think what it
69+
is to be a mother, there's a dear young lamb do.'
70+
71+
Apparently this consolatory perspective of a mother's prospects
72+
failed in producing its due effect. The patient shook her head,
73+
and stretched out her hand towards the child.
74+
75+
The surgeon deposited it in her arms. She imprinted her cold
76+
white lips passionately on its forehead; passed her hands over
77+
her face; gazed wildly round; shuddered; fell back--and died.
78+
They chafed her breast, hands, and temples; but the blood had
79+
stopped forever. They talked of hope and comfort. They had been
80+
strangers too long.
81+
82+
'It's all over, Mrs. Thingummy!' said the surgeon at last.
83+
84+
'Ah, poor dear, so it is!' said the nurse, picking up the cork of
85+
the green bottle, which had fallen out on the pillow, as she
86+
stooped to take up the child. 'Poor dear!'
87+
88+
'You needn't mind sending up to me, if the child cries, nurse,'
89+
said the surgeon, putting on his gloves with great deliberation.
90+
'It's very likely it WILL be troublesome. Give it a little gruel
91+
if it is.' He put on his hat, and, pausing by the bed-side on
92+
his way to the door, added, 'She was a good-looking girl, too;
93+
where did she come from?'
94+
95+
'She was brought here last night,' replied the old woman, 'by the
96+
overseer's order. She was found lying in the street. She had
97+
walked some distance, for her shoes were worn to pieces; but
98+
where she came from, or where she was going to, nobody knows.'
99+
100+
The surgeon leaned over the body, and raised the left hand. 'The
101+
old story,' he said, shaking his head: 'no wedding-ring, I see.
102+
Ah! Good-night!'
103+
104+
The medical gentleman walked away to dinner; and the nurse,
105+
having once more applied herself to the green bottle, sat down on
106+
a low chair before the fire, and proceeded to dress the infant.
107+
108+
What an excellent example of the power of dress, young Oliver
109+
Twist was! Wrapped in the blanket which had hitherto formed his
110+
only covering, he might have been the child of a nobleman or a
111+
beggar; it would have been hard for the haughtiest stranger to
112+
have assigned him his proper station in society. But now that he
113+
was enveloped in the old calico robes which had grown yellow in
114+
the same service, he was badged and ticketed, and fell into his
115+
place at once--a parish child--the orphan of a workhouse--the
116+
humble, half-starved drudge--to be cuffed and buffeted through
117+
the world--despised by all, and pitied by none.
118+
119+
Oliver cried lustily. If he could have known that he was an
120+
orphan, left to the tender mercies of church-wardens and
121+
overseers, perhaps he would have cried the louder.

project-euler/40-pandigital_prime.rb

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,31 @@
88

99
# Solution by Sam Gerber
1010

11-
def pandigital_prime
12-
primes = primes(987_654_321)
13-
puts "primes done"
14-
15-
loop do
16-
number = primes.pop
17-
digits = number.to_s.length
18-
return number if is_pandigital?(number, digits)
19-
end
20-
end
11+
# Note: all numbers whose digits sum to a multiple of 3 are divisible by 3.
12+
# The following are the sums of the digits of n-digit pandigital numbers:
13+
# 2-digits: 3 = 1 + 2 DIVISIBLE BY 3!
14+
# 3-digits: 6 = 1 + 2 + 3 DIVISIBLE BY 3!
15+
# 4-digits: 10 = 1 + 2 + 3 + 4 24 possible primes
16+
# 5-digits: 15 = 1 + 2 + 3 + 4 + 5 DIVISIBLE BY 3!
17+
# 6-digits: 21 = 1 + 2 + 3 + 4 + 5 + 6 DIVISIBLE BY 3!
18+
# 7-digits: 28 = 1 + 2 + 3 + 4 + 5 + 6 + 7 5040 possible primes
19+
# 8-digits: 36 = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 DIVISIBLE BY 3!
20+
# 9-digits: 45 = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 DIVISIBLE BY 3!
21+
22+
# With this knowledge, we simply build and sort an array of all possible
23+
# 4- or 7-digit pandigital numbers and beginning at the end, return the
24+
# first prime we find.
2125

26+
def pandigital_prime
27+
pandigitals = []
28+
[4,3,2,1].permutation{|p| pandigitals << p.join.to_i}
29+
[7,6,5,4,3,2,1].permutation{|p| pandigitals << p.join.to_i}
30+
pandigitals.sort!.reverse!
2231

23-
# This method retuns an array of all primes not exceeding max
24-
def primes(max)
25-
primes = []
26-
number = 2
27-
28-
while number <= max
29-
primes << number if is_prime? number
30-
number += 1
31-
end
32-
primes
32+
pandigitals.each{|number| return number if is_prime?(number)}
3333
end
3434

35+
3536
# This method returns true if a number is prime
3637
def is_prime?(number)
3738
number = number.abs
@@ -47,11 +48,4 @@ def is_prime?(number)
4748
true
4849
end
4950

50-
# This method returns true if the numbers contain all the
51-
# digits from 1 to n
52-
def is_pandigital?(*numbers, n)
53-
numbers = numbers.join.split("").sort
54-
numbers.count == numbers.uniq.count && numbers.first == "1" && numbers.last == n.to_s
55-
end
56-
5751
puts pandigital_prime
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Sub-string divisibility
2+
# Problem 43
3+
# The number, 1406357289, is a 0 to 9 pandigital number because it is made up of
4+
# each of the digits 0 to 9 in some order, but it also has a rather
5+
# interesting sub-string divisibility property.
6+
7+
# Let d1 be the 1st digit, d2 be the 2nd digit, and so on.
8+
# In this way, we note the following:
9+
10+
# d2d3d4=406 is divisible by 2
11+
# d3d4d5=063 is divisible by 3
12+
# d4d5d6=635 is divisible by 5
13+
# d5d6d7=357 is divisible by 7
14+
# d6d7d8=572 is divisible by 11
15+
# d7d8d9=728 is divisible by 13
16+
# d8d9d10=289 is divisible by 17
17+
# Find the sum of all 0 to 9 pandigital numbers with this property.
18+
19+
# Solution by Sam Gerber
20+
21+
def solution
22+
23+
d8_10s = []
24+
d7_10s = []
25+
d6_10s = []
26+
d5_10s = []
27+
d4_10s = []
28+
d3_10s = []
29+
d2_10s = []
30+
d1_10s = []
31+
17.step(987, 17) do |n|
32+
n = n.to_s.split("").to_a
33+
n = ["0"] + n if n.count == 2
34+
d8_10s << n if n.count == n.uniq.count
35+
end
36+
37+
d8_10s.uniq.each do |d8_10|
38+
d8_10[0..1].join.to_i.step(987, 100) do |n|
39+
d7 = (n/100).to_s
40+
if n % 13 == 0 && !d8_10.include?(d7)
41+
d7_10s << [d7] + d8_10
42+
end
43+
end
44+
end
45+
46+
47+
d7_10s.uniq.each do |d7_10|
48+
d7_10[0..1].join.to_i.step(987, 100) do |n|
49+
d6 = (n/100).to_s
50+
if n % 11 == 0 && !d7_10.include?(d6)
51+
d6_10s << [d6] + d7_10
52+
end
53+
end
54+
end
55+
56+
d6_10s.uniq.each do |d6_10|
57+
d6_10[0..1].join.to_i.step(987, 100) do |n|
58+
d5 = (n/100).to_s
59+
if n % 7 == 0 && !d6_10.include?(d5)
60+
d5_10s << [d5] + d6_10
61+
end
62+
end
63+
end
64+
65+
d5_10s.uniq.each do |d5_10|
66+
d5_10[0..1].join.to_i.step(987, 100) do |n|
67+
d4 = (n/100).to_s
68+
if n % 5 == 0 && !d5_10.include?(d4)
69+
d4_10s << [d4] + d5_10
70+
end
71+
end
72+
end
73+
74+
d4_10s.uniq.each do |d4_10|
75+
d4_10[0..1].join.to_i.step(987, 100) do |n|
76+
d3 = (n/100).to_s
77+
if n % 3 == 0 && !d4_10.include?(d3)
78+
d3_10s << [d3] + d4_10
79+
end
80+
end
81+
end
82+
83+
d3_10s.uniq.each do |d3_10|
84+
d3_10[0..1].join.to_i.step(987, 100) do |n|
85+
d2 = (n/100).to_s
86+
if n % 2 == 0 && !d3_10.include?(d2)
87+
d2_10s << [d2] + d3_10
88+
end
89+
end
90+
end
91+
92+
d2_10s.uniq.each do |d2_10|
93+
10.times do |d1|
94+
if !d2_10.include?(d1.to_s)
95+
d1_10s << ([d1] + d2_10).join.to_i
96+
end
97+
end
98+
end
99+
100+
d1_10s
101+
end
102+
103+
numbers = solution
104+
print numbers
105+
print numbers.inject(:+)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Pentagon numbers
2+
# Problem 44
3+
# Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2.
4+
# The first ten pentagonal numbers are:
5+
6+
# 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
7+
8+
# It can be seen that P4 + P7 = 22 + 70 = 92 = P8.
9+
# However, their difference, 70 − 22 = 48, is not pentagonal.
10+
11+
# Find the pair of pentagonal numbers, Pj and Pk, for which their sum and
12+
# difference are pentagonal and D = |Pk − Pj| is minimised;
13+
# what is the value of D?
14+
15+
# Solution by Sam Gerber
16+
17+
def solution
18+
pentagon_numbers = [1, 5]
19+
results = []
20+
21+
loop do
22+
# Add next term to the list
23+
n = pentagon_numbers.count + 1
24+
c = n * (3 * n - 1) / 2
25+
puts "#{n}: #{c}"
26+
pentagon_numbers.reverse_each do |b|
27+
break if b < c / 2
28+
a = c - b
29+
z = b - a
30+
if Math.sqrt(24 * a + 1) % 6 == 5 && pentagon_numbers.include?(z)
31+
return [z, a, b, c]
32+
end
33+
end
34+
pentagon_numbers << c
35+
#break if results.count > 10
36+
end
37+
results
38+
end
39+
40+
puts "solutions"
41+
print solution

0 commit comments

Comments
 (0)