下面代碼換成Ruby該怎么寫

fireflyman 2009-08-05
public class Multiple {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int sum = 0;
		for(int i=3;i<1000;i++){
			// 避免3和5的公倍数重复相加求和
			if(i%3==0 || i%5==0){
				sum += i;
			}
		}
		System.out.println(sum);
	}

}
Hooopo 2009-08-05
p (3..999).inject{|sum,n| sum + (n.modulo(3).zero? || n.modulo(5).zero? ? n : 0) }
Global site tag (gtag.js) - Google Analytics