0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

paizaラーニングレベルアップ問題集の「階乗の計算」「階乗の末尾に0はいくつ付く?」をやってみました。


問題
階乗の計算

階乗の末尾に0はいくつ付く?


方針

階乗の末尾に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
階乗の計算
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int factorial = n;
    while (--n) factorial *= n;
    printf("%d\n", factorial);
    return 0;
}
階乗の末尾に0はいくつ付く?
#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++
階乗の計算
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int factorial = n;
    while (--n) factorial *= n;
    cout << factorial << endl;
    return 0;
}
階乗の末尾に0はいくつ付く?
#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#
階乗の計算
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はいくつ付く?
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
階乗の計算
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はいくつ付く?
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
階乗の計算
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はいくつ付く?
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
階乗の計算
let n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let factorial = n;
while (--n) factorial *= n;
console.log(factorial);
階乗の末尾に0はいくつ付く?
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
階乗の計算
fun main() {
    val n = readLine()!!.toInt()
    var factorial = 1
    for (i in 1..n) factorial *= i
    println(factorial)
}
階乗の末尾に0はいくつ付く?
fun main() {
    var n = readLine()!!.toInt()
    var zero = 0
    while (n >= 5) {
        n /= 5
        zero += n
    }
    println(n)
}

PHP
階乗の計算
<?php
    $n = intval(fgets(STDIN));
    $factorial = $n;
    while (--$n) $factorial *= $n;
    echo $factorial, PHP_EOL;
?>
階乗の末尾に0はいくつ付く?
<?php
    $n = intval(fgets(STDIN));
    $zero = 0;
    while ($n >= 5) {
        $n = intdiv($n, 5);
        $zero += $n;
    }
    echo $zero, PHP_EOL;
?>

Perl
階乗の計算
my $n = int(<STDIN>);
my $factorial = $n;
while (--$n) {
    $factorial *= $n;
}
print "$factorial$/";
階乗の末尾に0はいくつ付く?
my $n = int(<STDIN>);
my $zero = 0;
while ($n >= 5) {
    $n = int($n / 5);
    $zero += $n;
}
print "$zero$/";

Python3
階乗の計算
n = int(input())
factorial = 1
for i in range(1, n + 1):
    factorial *= i
print(factorial)
階乗の末尾に0はいくつ付く?
n = int(input())
zero = 0
while n >= 5:
    n //= 5
    zero += n
print(zero)

Ruby
階乗の計算
n = gets.to_i
factorial = 1
(1..n).each do |i|
    factorial *= i
end
p factorial
階乗の末尾に0はいくつ付く?
n = gets.to_i
zero = 0
while n >= 5
    n /= 5
    zero += n
end
p zero

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はいくつ付く?
import scala.io.StdIn._

object Main extends App{
    var n = readInt()
    var zero = 0
    while (n >= 5) {
        n /= 5
        zero += n
    }
    println(zero)
}

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?