6. public static int Problem1(int limit)
{
int sum = 0;
for (int i = 1; i < limit; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
7. Public Function Problem1(ByVal limit As Integer)
Dim sum = 0
For i = 1 To limit - 1
If i Mod 3 = 0 OrElse i Mod 5 = 0 Then
sum += i
End If
Next
Return sum
End Function
8. public static int Problem2(int limit)
{
int sum = 0;
int a = 1;
int b = 1;
while (b < limit)
{
if (b % 2 == 0)
{
sum += b;
}
var temp = a;
a = b;
b = a + temp;
}
return sum;
}
9. Public Function Problem2(ByVal limit As Integer)
Dim sum = 0
Dim a = 1
Dim b = 1
While b < limit
If b Mod 2 = 0 Then
sum += b
End If
Dim temp = a
a = b
b = a + temp
End While
Return sum
End Function
10. public static int Problem1(int limit)
{
int sum = 0;
for (int i = 1; i < limit; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
数列生成
終了条件
抽出条件
合計処理
11. public static int Problem2(int limit)
{
int sum = 0;
int a = 1;
int b = 1;
while (b < limit)
{
if (b % 2 == 0)
{
sum += b;
}
var temp = a;
a = b;
b = a + temp;
}
return sum;
}
数列生成
終了条件
抽出条件
合計処理
19. public static IEnumerable<int> GetNaturalNumber()
{
int i = 0;
while (true)
{
yield return ++i;
}
}
public static IEnumerable<int> GetFibonacchiNumber()
{
int a = 1;
int b = 1;
while (true)
{
yield return b;
var temp = a;
a = b;
b = a + temp;
}
}
20. Public Iterator Function GetNaturalNumber() As IEnumerable(Of Integer)
Dim i As Integer = 0
While True
i = i + 1
Yield i
End While
End Function
Public Iterator Function GetFibonacchi() As IEnumerable(Of Integer)
Dim a = 1
Dim b = 1
While True
Yield b
Dim temp = a
a = b
b += temp
End While
End Function
21. public static int Problem1(int limit)
{
int sum = 0;
foreach (var i in GetNaturalNumber())
{
if (i < limit)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
else
{
break;
}
}
return sum;
}
数列生成
終了条件
抽出条件
合計処理
22. public static int Problem2(int limit)
{
int sum = 0;
foreach (var i in GetFibonacchiNumber())
{
if (i < limit)
{
if (i % 2 == 0)
{
sum += i;
}
}
else
{
break;
}
}
return sum;
}
数列生成
終了条件
抽出条件
合計処理
27. public static int Problem1(int limit)
{
return GetNaturalNumber()
.TakeWhile(i => i < limit)
.Where(i => i % 3 == 0 || i % 5 == 0)
.Sum();
}
public static int Problem2(int limit)
{
return GetFibonacchiNumber()
.TakeWhile(i => i < limit)
.Where(i => i % 2 == 0)
.Sum();
}
28. Public Function Problem1(ByVal limit As Integer) As Integer
Return GetNaturalNumber().
TakeWhile(Function(i) i < limit).
Where(Function(i) i Mod 3 = 0 OrElse i Mod 5 = 0).
Sum()
End Function
Public Function Problem2(ByVal limit As Integer) As Integer
Return GetFibonacchi().
TakeWhile(Function(i) i < limit).
Where(Function(i) i Mod 2).
Sum()
End Function