For example, if you print the sum and most common condition that can be divisible by 2 and 3 in 1-101, you may write TheSum0forxinxrange (): ifx % 20 andx % 30: theSum + xprintTheSum: for example, the sum of sums and
The most common condition may be written in this way.
TheSum=0for x in xrange(101): if x %2==0 and x%3==0: TheSum+=xprint TheSum
Reply content: No
J
+/ (#~ (0=2&|) *. (0=3&|)) 1+i.100
Scala:
6 to 100 by 6 sum
Think about readability. sum (range (6,101, 6 ))
1 to(100) asList select(%6==0) sum
Haskell: sum [x | x <-[1 .. 100], x 'mod' 6 = 0]
sum [x | x <- [1..101], x `mod` 2 == 0 && x `mod` 3 == 0]
It can be divided by 2 and 3, not 6.
Sum [6, 12 .. 101] Ruby is also easy
(1..100).select{|x| x%6 == 0 }.inject(:+)
This kind of thing is more important than syntactic sugar and standard library... Sum [6, 12 .. 100]
Which of the following statements about Haskell... Let's write it like this...
Sum. filter (= 0). ('mod' 6) $[1 .. 100]
There are also those who have written [1 .. 100] as [0 .. 101! Have you reviewed any questions? I know how to play with it...