回答編集履歴

3

奇数の和の範囲の誤りを訂正

2019/10/08 10:49

投稿

xebme
xebme

スコア1083

test CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- の和 (1 から n) n個 : n * (n + 1) / 2
5
+ 自然数の和 (1 から n) n個 : n * (n + 1) / 2
6
6
 
7
7
  偶数の和 (2 から 2n) n個 : n * (n + 1)
8
8
 
9
- 奇数の和 (1 から 2n+1) n個 : n ^ 2
9
+ 奇数の和 (1 から 2n-1) n個 : n ^ 2
10
10
 
11
11
 
12
12
 
@@ -18,7 +18,7 @@
18
18
 
19
19
  ```Java
20
20
 
21
- n -> n*(n+1)/2 // と偶数
21
+ n -> n*(n+1)/2 // 自然
22
22
 
23
23
  n -> (int)Math.pow(Math.ceil(n/2.0), 2)) // 奇数
24
24
 
@@ -36,7 +36,7 @@
36
36
 
37
37
  ```Java
38
38
 
39
- BiFunction<Integer,Integer,Integer> diff(IntFunction<Integer> f) {
39
+ BiFunction<Integer,Integer,Integer> rangeSum(IntFunction<Integer> f) {
40
40
 
41
41
  return (x,y) -> f.apply(y) - f.apply(x-1);
42
42
 
@@ -54,10 +54,60 @@
54
54
 
55
55
  ```Java
56
56
 
57
- diff(n -> n*(n+1)/2).apply(1,5); // と偶数
57
+ rangeSum(n -> n*(n+1)/2).apply(1,5); // 自然
58
58
 
59
- diff(n -> (int)Math.pow(Math.ceil(n/2.0), 2))).apply(1,5); // 奇数
59
+ rangeSum(n -> (int)Math.pow(Math.ceil(n/2.0), 2))).apply(1,5); // 奇数
60
60
 
61
- diff(n -> (n/2)*(n/2)+(n/2)).apply(1,5);// 偶数
61
+ rangeSum(n -> (n/2)*(n/2)+(n/2)).apply(1,5);// 偶数
62
62
 
63
63
  ```
64
+
65
+
66
+
67
+ ###
68
+
69
+ **訂正 2019-10-08**
70
+
71
+ 奇数の和の範囲の誤りを訂正しました。
72
+
73
+ diffの名称をrangeSumに変更しました。
74
+
75
+
76
+
77
+ **追記 2019-10-08**
78
+
79
+
80
+
81
+ **合成関数(y < x にも対応)**
82
+
83
+
84
+
85
+ ```Java
86
+
87
+ BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(Integer::compare);
88
+
89
+ BinaryOperator<Integer> minBy = BinaryOperator.minBy(Integer::compare);
90
+
91
+ BiFunction<Integer,Integer,Integer> rangeSum(IntFunction<Integer> f) {
92
+
93
+ return (x,y) -> f.apply(maxBy.apply(x,y)) - f.apply(minBy.apply(x,y)-1);
94
+
95
+ }
96
+
97
+ ```
98
+
99
+
100
+
101
+ **和の計算**
102
+
103
+
104
+
105
+ ```Java
106
+
107
+ rangeSum(n -> n*(n+1)*(2*n+1)/6).apply(1,5); // 自然数の二乗の和
108
+
109
+ rangeSum(n -> (int)Math.pow(n*(n+1)/2, 2)).apply(1,5); // 自然数の三乗の和
110
+
111
+
112
+
113
+ ```

2

ラムダ式を修正

2019/10/08 10:49

投稿

xebme
xebme

スコア1083

test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
  ```Java
56
56
 
57
- diff(n -> n*(n+1)/2)).apply(1,5); // 奇数と偶数
57
+ diff(n -> n*(n+1)/2).apply(1,5); // 奇数と偶数
58
58
 
59
59
  diff(n -> (int)Math.pow(Math.ceil(n/2.0), 2))).apply(1,5); // 奇数
60
60
 

1

ラムダ式の修正

2019/09/28 08:33

投稿

xebme
xebme

スコア1083

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  ```Java
20
20
 
21
- n -> n*(n+1)/2) // 奇数と偶数
21
+ n -> n*(n+1)/2 // 奇数と偶数
22
22
 
23
23
  n -> (int)Math.pow(Math.ceil(n/2.0), 2)) // 奇数
24
24