-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy path6_array.rb
More file actions
30 lines (28 loc) · 862 Bytes
/
6_array.rb
File metadata and controls
30 lines (28 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Write a method named prime_chars? which takes array of strings
# and returns true if the sum of the characters is prime.
#
# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself.
#
# Examples of length three
# prime_chars? ['abc'] # => true
# prime_chars? ['a', 'bc'] # => true
# prime_chars? ['ab', 'c'] # => true
# prime_chars? ['a', 'b', 'c'] # => true
#
# Examples of length four
# prime_chars? ['abcd'] # => false
# prime_chars? ['ab', 'cd'] # => false
# prime_chars? ['a', 'bcd'] # => false
# prime_chars? ['a', 'b', 'cd'] # => false
class Integer
def prime?
return false if self < 2
2.upto Math.sqrt(self) do |i|
return false if self % i == 0
end
true
end
end
def prime_chars?(strings)
strings.join.length.prime?
end