Skip to main content
corrected equations
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

\$1634 = 14^4 + 64^4 + 34^4 + 44^4\$\$1634 = 1^4 + 6^4 + 3^4 + 4^4\$
\$8208 = 84^4 + 24^4 + 04^4 + 84^4\$\$8208 = 8^4 + 2^4 + 0^4 + 8^4\$
\$9474 = 94^4 + 44^4 + 74^4 + 44^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 + 9474 = 19316\$.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

\$1634 = 14^4 + 64^4 + 34^4 + 44^4\$
\$8208 = 84^4 + 24^4 + 04^4 + 84^4\$
\$9474 = 94^4 + 44^4 + 74^4 + 44^4\$

As \$1 = 1^4\$ is not a sum it is not included. The sum of these numbers is \$1634 + 8208 + 9474 = 19316\$.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

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 + 9474 = 19316\$.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?
math-jaxed the numbers.
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

\$1634 = 14^4 + 64^4 + 34^4 + 44^4\$
\$8208 = 84^4 + 24^4 + 04^4 + 84^4\$
\$9474 = 94^4 + 44^4 + 74^4 + 44^4\$

As 1 = 1^4\$1 = 1^4\$ is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316\$1634 + 8208 + 9474 = 19316\$.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 1^4 is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

\$1634 = 14^4 + 64^4 + 34^4 + 44^4\$
\$8208 = 84^4 + 24^4 + 04^4 + 84^4\$
\$9474 = 94^4 + 44^4 + 74^4 + 44^4\$

As \$1 = 1^4\$ is not a sum it is not included. The sum of these numbers is \$1634 + 8208 + 9474 = 19316\$.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?
added 1 character in body
Source Link
ajn123
  • 201
  • 1
  • 3

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 141^4 is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?

This is for Project Euler #30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 1^4 is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


list = []
2.upto(1_000_000) do |num|
 list << num if num.to_s.split("").map(&:to_i).map { |num| num ** 5 }.inject(:+) == num
end

puts list.inject(:+)

This works, and I am here for three reasons:

  1. I use map twice, and this smells but I don't know an easier/better way.
  2. How does the & work? Notice I do map(&:to_i) and inject(:+). I have seen examples of this but don't know why they are different and what is going on.
  3. Any suggestions on code improvements?
added 8 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
added 13 characters in body
Source Link
ajn123
  • 201
  • 1
  • 3
Loading
Source Link
ajn123
  • 201
  • 1
  • 3
Loading