paizaラーニングレベルアップ問題集の「階乗の計算」をやってみた。

paizaラーニングレベルアップ問題集の「[階乗の計算](https://paiza.jp/works/mondai/loop_problems2/loop_problems2__factorial)」「[階乗の末尾に0はいくつ付く?](https://paiza.jp/works/mondai/loop_problems2/loop_problems2__factorial_zero)」をやってみました。
- - -
##### 問題
###### 階乗の計算
https://paiza.jp/works/mondai/loop_problems2/loop_problems2__factorial
###### 階乗の末尾に0はいくつ付く?
https://paiza.jp/works/mondai/loop_problems2/loop_problems2__factorial_zero
- - -
##### 方針
階乗の末尾に0はいくつ付くか、ですが、
- $5$の倍数を掛けると$0$が一つ付きます
- 更に$25(=5\times5)$の倍数を掛けると更に$0$が一つ（合計2つ）付きます
- 更に$125(=5\times5\times5)$の倍数を掛けると更に$0$が一つ（合計3つ）付きます
- 更に$625(=5\times5\times5\times5)$の倍数を掛けると更に$0$が一つ（合計4つ）付きます

「$N$を$5$で割った商を足して、$N$を$5$で割った商に置き換える」
操作を繰り返します。
- - -
##### C
###### 階乗の計算
```c
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int factorial = n;
    while (--n) factorial *= n;
    printf("%d\n", factorial);
    return 0;
}
```
###### 階乗の末尾に0はいくつ付く?
```c
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int zero = 0;
    while (n >= 5) {
        n /= 5;
        zero += n;
    }
    printf("%d\n", zero);
    return 0;
}
```
- - -
##### C++
###### 階乗の計算
```cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int factorial = n;
    while (--n) factorial *= n;
    cout << factorial << endl;
    return 0;
}
```
###### 階乗の末尾に0はいくつ付く?
```cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int zero = 0;
    while (n >= 5) {
        n /= 5;
        zero += n;
    }
    cout << zero << endl;
    return 0;
}
```
- - -
##### C#
###### 階乗の計算
```cs
using System;

class Program
{
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int factorial = 1;
        for (int i = 1; i <= n; i++) factorial *= i;
        Console.WriteLine(factorial);
    }
}
```
###### 階乗の末尾に0はいくつ付く?
```cs
using System;

class Program
{
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int zero = 0;
        while (n >= 5) {
            n /= 5;
            zero += n;
        }
        Console.WriteLine(zero);
    }
}
```
- - -
##### Go
###### 階乗の計算
```go
package main
import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    factorial := 1
    for i := 1; i <= n; i++ {
        factorial *= i
    }
    fmt.Println(factorial)
}
```
###### 階乗の末尾に0はいくつ付く?
```go
package main
import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    zero := 0
    for n >= 5 {
        n /= 5
        zero += n
    }
    fmt.Println(zero)
}
```
- - -
##### Java
###### 階乗の計算
```java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int factorial = 1;
        for (int i = 1; i <= n; i++) factorial *= i;
        System.out.println(factorial);
    }
}
```
###### 階乗の末尾に0はいくつ付く?
```java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int zero = 0;
        while (n >= 5) {
            n /= 5;
            zero += n;
        }
        System.out.println(zero);
    }
}
```
- - -
##### JavaScript
###### 階乗の計算
```js
let n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let factorial = n;
while (--n) factorial *= n;
console.log(factorial);
```
###### 階乗の末尾に0はいくつ付く?
```js
let n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let zero = 0;
while (n >= 5) {
    n = parseInt(n / 5);
    zero += n;
}
console.log(zero);
```
- - -
##### Kotlin
###### 階乗の計算
```kotlin
fun main() {
    val n = readLine()!!.toInt()
    var factorial = 1
    for (i in 1..n) factorial *= i
    println(factorial)
}
```
###### 階乗の末尾に0はいくつ付く?
```kotlin
fun main() {
    var n = readLine()!!.toInt()
    var zero = 0
    while (n >= 5) {
        n /= 5
        zero += n
    }
    println(n)
}
```
- - -
##### PHP
###### 階乗の計算
```php
<?php
    $n = intval(fgets(STDIN));
    $factorial = $n;
    while (--$n) $factorial *= $n;
    echo $factorial, PHP_EOL;
?>
```
###### 階乗の末尾に0はいくつ付く?
```php
<?php
    $n = intval(fgets(STDIN));
    $zero = 0;
    while ($n >= 5) {
        $n = intdiv($n, 5);
        $zero += $n;
    }
    echo $zero, PHP_EOL;
?>
```
- - -
##### Perl
###### 階乗の計算
```pl
my $n = int(<STDIN>);
my $factorial = $n;
while (--$n) {
    $factorial *= $n;
}
print "$factorial$/";
```
###### 階乗の末尾に0はいくつ付く?
```pl
my $n = int(<STDIN>);
my $zero = 0;
while ($n >= 5) {
    $n = int($n / 5);
    $zero += $n;
}
print "$zero$/";
```
- - -
##### Python3
###### 階乗の計算
```py
n = int(input())
factorial = 1
for i in range(1, n + 1):
    factorial *= i
print(factorial)
```
###### 階乗の末尾に0はいくつ付く?
```py
n = int(input())
zero = 0
while n >= 5:
    n //= 5
    zero += n
print(zero)
```
- - -
##### Ruby
###### 階乗の計算
```rb
n = gets.to_i
factorial = 1
(1..n).each do |i|
    factorial *= i
end
p factorial
```
###### 階乗の末尾に0はいくつ付く?
```rb
n = gets.to_i
zero = 0
while n >= 5
    n /= 5
    zero += n
end
p zero
```
- - -
##### Scala
###### 階乗の計算
```scala
import scala.io.StdIn._

object Main extends App{
    val n = readInt()
    var factorial = 1
    for (i <- 1 to n) factorial *= i
    println(factorial)
}
```
###### 階乗の末尾に0はいくつ付く?
```scala
import scala.io.StdIn._

object Main extends App{
    var n = readInt()
    var zero = 0
    while (n >= 5) {
        n /= 5
        zero += n
    }
    println(zero)
}
```
- - -
##### Swift
###### 階乗の計算
```swift
let n = Int(readLine()!)!
var factorial = 1
for i in 1...n {
    factorial *= i
}
print(factorial)
```
###### 階乗の末尾に0はいくつ付く?
```swift
var n = Int(readLine()!)!
var zero = 0
while n >= 5 {
    n /= 5
    zero += n
}
print(zero)
```