This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
coding-corner/2017-03-21/jo-so_euler87.rb

30 lines
689 B
Ruby
Raw Normal View History

2017-03-22 01:41:00 +01:00
#!/usr/bin/ruby -W
# coding: utf-8
#
# Copyright © 2017 Jörg Sommer <joerg@alea.gnuu.de>
#
# License: MIT https://opensource.org/licenses/MIT
######
##
## Solution of Euler 87 https://projecteuler.net/problem=87
##
require 'prime'
LIMIT = 50_000_000
primesPower2 = Prime.each(Math.sqrt(LIMIT)).map { |e| e ** 2 }
primesPower3 = Prime.each(Math.cbrt(LIMIT)).map { |e| e ** 3 }
primesPower4 = Prime.each(LIMIT ** (1.0/4)).map { |e| e ** 4 }
ans = primesPower2.product(primesPower3) \
.map { |a, b| a + b } \
.select { |e| e < LIMIT } \
.product(primesPower4) \
.map { |a, b| a + b } \
.select { |e| e < LIMIT } \
.uniq \
.size
puts 'Answer: ' + ans.to_s