繁体   English   中英

如何打印平方除以5的前10个自然数?

[英]How to print first 10 natural numbers who's square is divisible by 5?

我目前正在看这张幻灯片: http : //www.slideshare.net/GeisonFlores/ruby-functional-programming

幻灯片24显示,通过执行以下操作,我可以找到平方的前10个自然数可以被5整除:

Integer::natural.select{ |x| x**2 % 5 == 0}.take(10).inject(:+)

我收到错误消息:找不到整数的“自然”类型。 我尝试使用ruby 1.9.3和2.2,但似乎无法运行此LOC。

谁能指出我该如何纠正这个问题? 我是FP新手。

在其他幻灯片上定义了Integer::natural 在执行select之前,运行以下代码:

class Integer
 def self.natural
   Enumerator.new do |yielder|
    (1..1.0/0).each do |number|
     yielder.yield number
    end
   end
 end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM