Permalink
Browse files

Solve problem 30 and 54

  • Loading branch information...
1 parent fd96070 commit a14a6f7c16628d5d1c893f0ebda4ceab67d547c5 Austin Lee committed Mar 28, 2012
Showing with 1,226 additions and 1 deletion.
  1. +7 −0 src/problem-30/problem
  2. +20 −0 src/problem-30/pzoe.rb
  3. +16 −0 src/problem-30/pzvz.rb
  4. +1 −1 src/problem-5/uack.rb
  5. +161 −0 src/problem-54/kpvj.rb
  6. +1,000 −0 src/problem-54/poker
  7. +21 −0 src/problem-54/problem
View
@@ -0,0 +1,7 @@
+Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
+1634 = 1^4 + 6^4 + 3^4 + 4^4
+8208 = 8^4 + 2^4 + 0^4 + 8^4
+9474 = 9^4 + 4^4 + 7^4 + 4^4
+As 1 = 1^4 is not a sum it is not included.
+The sum of these numbers is 1634 + 8208 + 9479 = 19316.
+Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
View
@@ -0,0 +1,20 @@
+public
+
+# since we know that the max is 1000000, let's try iterating over numbers this time.
+def pzoe
+ sum = 0
+ (0..9).each do |a|
+ (0..9).each do |b|
+ (0..9).each do |c|
+ (0..9).each do |e|
+ (0..9).each do |f|
+ (0..9).each do |g|
+ sum += a * 100000 + b * 10000 + c * 1000 + e * 100 + f * 10 + g if a * 100000 + b * 10000 + c * 1000 + e * 100 + f * 10 + g == a ** 5 + b ** 5 + c ** 5 + e ** 5 + f ** 5 + g ** 5
+ end
+ end
+ end
+ end
+ end
+ end
+ sum - 1
+end
View
@@ -0,0 +1,16 @@
+public
+
+# note: 9^5 is 59049. 9999 would be 236196 and 99999 would be 295245. Thus, anything above 1000000 would most likely have a sum that's too high.
+def pzvz
+ sum = 0
+ (10..1000000).each do |i|
+ csum = 0
+ comp = i
+ while i > 0
+ csum += (i % 10) ** 5
+ i /= 10
+ end
+ sum += comp if comp == csum
+ end
+ sum
+end
View
@@ -1,7 +1,7 @@
public
def uack
- # Same as uacs but now let's try to code the process
+ # same as uacc but uses arrPrimeFactor2 instead, it's a slight improvement
neededMultiples = Array.new
(1 .. 20).each do |i|
dupMultiples = Array.new(neededMultiples)
View
@@ -0,0 +1,161 @@
+public
+
+def kpvj
+ result= 0
+ File.open("src/problem-54/poker").each do |line|
+ a = Hand.new(line[0,14])
+ b = Hand.new(line[15,30])
+ aS = a.getScore
+ bS = b.getScore
+ result += 1 if aS > bS
+ if aS == bS
+ if aS == 0
+ result += 1 if a.getSingleScore > b.getSingleScore
+ else
+ result += 1 if a.getDoubleScore > b.getDoubleScore
+ end
+ end
+ end
+ result
+end
+
+private
+
+class Hand
+ attr_accessor :score
+ def initialize(stringhand)
+ @cards = stringhand.split(' ').map {|card| Card.new(card)}
+ @score = 0
+ end
+ def getScore
+ @score += numOfPairs.to_i
+ @score += 4 if threeOfAKind?
+ @score += 8 if straight?
+ @score += 16 if flush?
+ @score += 32 if fullHouse?
+ @score += 64 if fourOfAKind?
+ @score += 128 if straightFlush?
+ @score += 256 if royalFlush?
+ @score
+ end
+ def getSingleScore
+ sum = 0
+ iterator = 1
+ fullHand = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
+ sortedHand = Array.new
+ malleable = @cards.collect{|c| c.value}
+ fullHand.each do |i|
+ sortedHand.push(i) if malleable.include?(i)
+ end
+ sortedHand.each do |i|
+ val = 0
+ if i.to_i != 0
+ val = i.to_i
+ elsif i == 'T'
+ val = 10
+ elsif i == 'J'
+ val = 11
+ elsif i == 'Q'
+ val = 12
+ elsif i == 'K'
+ val = 13
+ elsif i == 'A'
+ val = 14
+ end
+ sum += val * iterator
+ iterator *= 14
+ end
+ sum
+ end
+ def getDoubleScore
+ sum = 0
+ iterator = 1
+ fullHand = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
+ sortedHand = Array.new
+ malleable = @cards.collect{|c| c.value}
+ findDouble = 0
+ fullHand.each do |i|
+ sortedHand.push(i) if malleable.count(i) == 1
+ findDouble = i if malleable.count(i) == 2
+ end
+ sortedHand.push findDouble
+ sortedHand.each do |i|
+ val = 0
+ if i.to_i != 0
+ val = i.to_i
+ elsif i == 'T'
+ val = 10
+ elsif i == 'J'
+ val = 11
+ elsif i == 'Q'
+ val = 12
+ elsif i == 'K'
+ val = 13
+ elsif i == 'A'
+ val = 14
+ end
+ sum += val * iterator
+ iterator *= 14
+ end
+ sum
+ end
+ def numOfPairs
+ num = 0
+ malleable = @cards.collect{|c| c.value}
+ malleable.each do |i|
+ num += 1 if malleable.count(i) > 1
+ end
+ num/2
+ end
+ def threeOfAKind?
+ malleable = @cards.collect{|c| c.value}
+ (0..2).each do |i|
+ return true if malleable.count(malleable[i])== 3
+ end
+ false
+ end
+ def straight?
+ correctOrder = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
+ malleable = @cards.collect{|c| c.value}.sort
+ if malleable[0].to_i != 0
+ startOrder = correctOrder.index malleable[0]
+ (1..4).each do |i|
+ return false if not malleable.include?(correctOrder[startOrder + i])
+ end
+ else
+ startOrder = 8
+ (0..4).each do |i|
+ return false if not malleable.include?(correctOrder[startOrder + i])
+ end
+ end
+ true
+ end
+ def flush?
+ @cards.collect{|c|c.suit}.uniq.size == 1
+ end
+ def fullHouse?
+ return (self.threeOfAKind? and numOfPairs == 2)
+ end
+ def fourOfAKind?
+ malleable = @cards.collect{|c| c.value}
+ (0..1).each do |i|
+ return true if malleable.count(malleable[i]) == 4
+ end
+ false
+ end
+ def straightFlush?
+ return (self.straight? and self.flush?)
+ end
+ def royalFlush?
+ highcard = @cards.collect{|c|c.value}.include? 'A'
+ return (highcard && self.straightFlush?)
+ end
+end
+
+class Card
+ attr_accessor :suit, :value
+ def initialize (stringcard)
+ @suit = stringcard[1]
+ @value = stringcard[0]
+ end
+end
Oops, something went wrong.

0 comments on commit a14a6f7

Please sign in to comment.