paizaラーニングレベルアップ問題集の「二重ループメニュー」をやってみた。(4)

paizaラーニングレベルアップ問題集の「二重ループメニュー」をやってみました。
- - -
##### 問題
###### 行列の転置
https://paiza.jp/works/mondai/double_roop_problems/double_roop_problems__transposition
###### かけ算表
https://paiza.jp/works/mondai/double_roop_problems/double_roop_problems__multiplication
###### 素数の個数
https://paiza.jp/works/mondai/double_roop_problems/double_roop_problems__prime_number_easy
###### log2
https://paiza.jp/works/mondai/double_roop_problems/double_roop_problems__log2
- - -
- - -
##### 方針

###### 素数の個数
[エラトステネスのふるい](https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9)を使う方法もありますが、今回は「二重ループ」メニューなので、二重ループ（$O(N^2)$解法）で解きます。
※制約条件が$N\le 10000$の場合は、$O(N\sqrt N)$解法でないとタイムオーバーになります。
###### log2
[階乗の末尾に0はいくつ付く?](https://qiita.com/anonymously115/items/efefa445f48bb1e0e76f)の解き方（$O(\log N)$解法）もありますが、今回は「二重ループ」メニューなので、二重ループ（$O(N\log N)$解法）で解きます。
- - -
##### C
###### 行列の転置
```c
#include <stdio.h>

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	int A[n][k];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < k; j++)
			scanf("%d", &A[i][j]);
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < n; j++) {
			if (j) putchar(' ');
			printf("%d", A[j][i]);
		}
		puts("");
	}
	return 0;
}
```
###### かけ算表
```c
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j) putchar(' ');
			printf("%d", A[i] * A[j]);
		}
		puts("");
	}
	return 0;
}
```
###### 素数の個数
```c
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int cnt = 0;
	for (int k = 2; k <= n; k++) {
		for (int i = 2; i <= k; i++) {
			if (i == k) cnt++;
			else if (k % i == 0) break;
		}
	}
	printf("%d\n", cnt);
	return 0;
}
```
###### log2
```c
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int cnt = 0;
	for (int k = 1; k <= n; k++) 
		for (int i = 2; k % i == 0; i *= 2)
			cnt++;
	printf("%d\n", cnt);
	return 0;
}
```
- - -
##### C++
###### 行列の転置
```cpp
#include <iostream>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	int A[n][k];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < k; j++)
			cin >> A[i][j];
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < n; j++) {
			if (j) cout << ' ';
			cout << A[j][i];
		}
		cout << endl;
	}
	return 0;
}
```
###### かけ算表
```cpp
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int A[n];
	for (int i = 0; i < n; i++) cin >> A[i];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j) cout << ' ';
			cout << A[i] * A[j];
		}
		cout << endl;
	}
	return 0;
}
```
###### 素数の個数
```cpp
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int cnt = 0;
	for (int k = 2; k <= n; k++)
		for (int i = 2; i <= k; i++)
			if (i == k) cnt++;
			else if (k % i == 0) break;
	cout << cnt << endl;
	return 0;
}
```
###### log2
```cpp
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int cnt = 0;
	for (int k = 1; k <= n; k++)
		for (int i = 2; k % i == 0; i *= 2)
			cnt++
	cout << cnt << endl;
	return 0;
}
```
- - -
##### C#
###### 行列の転置
```cs
using System;

class Program
{
	public static void Main()
	{
		int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int n = nk[0], k = nk[1];
		int[][] A = new int[n][];
		for (int i = 0; i < n; i++) A[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		for (int i = 0; i < k; i++) {
			for (int j = 0; j < n; j++) {
				if (j > 0) Console.Write(' ');
				Console.Write(A[j][i]);
			}
			Console.WriteLine();
		}
	}
}
```
###### かけ算表
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (j > 0) Console.Write(' ');
				Console.Write(A[i] * A[j]);
			}
			Console.WriteLine();
		}
	}
}
```
###### 素数の個数
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int cnt = 0;
		for (int k = 2; k <= n; k++)
			for (int i = 2; i <= k; i++)
				if (i == k) cnt++;
				else if (k % i == 0) break;
		Console.WriteLine(cnt);
	}
}
```
###### log2
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int cnt = 0;
		for (int k = 2; k <= n; k++)
			for (int i = 2; k % i == 0; i *= 2)
				cnt++;
		Console.WriteLine(cnt);
	}
}
```
- - -
##### Go
###### 行列の転置
```go
package main
import "fmt"

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&A[i][j])
		}
	}
	for i := 0; i < k; i++ {
		for j := 0; j < n; j++ {
			if j > 0 {
				fmt.Print(" ")
			}
			fmt.Print(A[j][i])
		}
		fmt.Println()
	}
}
```
###### かけ算表
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if j > 0 {
				fmt.Print(" ")
			}
			fmt.Print(A[i] * A[j])
		}
		fmt.Println()
	}
}
```
###### 素数の個数
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	cnt := 0
	for k := 2; k <= n; k++ {
		for i := 2; i <= k; i++ {
			if i == k {
				cnt += 1
			} else if k % i == 0 {
				break
			}
		}
	}
	fmt.Println(cnt)
}
```
###### log2
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	cnt := 0
	for k := 1; k <= n; k++ {
		for i := 2; k % i == 0; i *= 2 {
			cnt += 1
		}
	}
	fmt.Println(cnt)
}
```
- - -
##### Java
###### 行列の転置
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		sc.nextLine();
		int[][] A = new int[n][];
		for (int i = 0; i < n; i++)
			A[i] = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		for (int i = 0; i < k; i++) {
			for (int j = 0; j < n; j++) {
				if (j > 0) System.out.print(' ');
				System.out.print(A[j][i]);
			}
			System.out.println();
		}
	}
}
```
###### かけ算表
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (j > 0) System.out.print(' ');
				System.out.print(A[i] * A[j]);
			}
			System.out.println();
		}
	}
}
```
###### 素数の個数
```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 cnt = 0;
		for (int k = 2; k <= n; k++)
			for (int i = 2; i <= k; i++)
				if (i == k) cnt++;
				else if (k % i == 0) break;
		System.out.println(cnt);

	}
}
```
###### log2
```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 cnt = 0;
		for (int k = 1; k <= n; k++)
			for (int i = 2; k % i == 0; i *= 2)
				cnt++;
		System.out.println(cnt);
	}
}
```
- - -
##### JavaScript
###### 行列の転置
```js
const [[n, k], ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
for (var i = 0; i < k; i++) {
	let s = "";
	for (var j = 0; j < n; j++) {
		if (j) s += ' ';
		s += A[j][i];
	}
	console.log(s);
}
```
###### かけ算表
```js
const [[n], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
for (var i = 0; i < n; i++) {
	let s = "";
	for (var j = 0; j < n; j++) {
		if (j) s += ' ';
		s += A[i] * A[j];
	}
	console.log(s);
}
```
###### 素数の個数
```js
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let cnt = 0;
for (var k = 2; k <= n; k++)
	for (var i = 2; i <= k; i++)
		if (i == k) cnt++;
		else if (k % i == 0) break;
console.log(cnt);
```
###### log2
```js
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let cnt = 0;
for (var k = 1; k <= n; k++)
	for (var i = 2; k % i == 0; i *= 2)
		cnt++;
console.log(cnt);
```
- - -
##### Kotlin
###### 行列の転置
```kotlin
fun main() {
	val (n, k) = readLine()!!.split(' ').map { it.toInt() }
	val A = Array(n) { readLine()!!.split(' ').map { it.toInt() } }
	for (i in 0 until k) {
		for (j in 0 until n) {
			if (j > 0) print(' ')
			print(A[j][i])
		}
		println()
	}
}
```
###### かけ算表
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	for (i in 0 until n) {
		for (j in 0 until n) {
			if (j > 0) print(' ')
			print(A[i] * A[j])
		}
		println()
	}
}
```
###### 素数の個数
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	var cnt = 0
	for (k in 2..n)
		for (i in 2..k)
			if (i == k) cnt += 1
			else if (k % i == 0) break
	println(cnt)
}
```
###### log2
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	var cnt = 0
	for (k in 1..n) {
		var i = 2
		while (k % i == 0) {
			cnt += 1
			i *= 2
		}
	}
	println(cnt)
}
```
- - -
##### PHP
###### 行列の転置
```php
<?php
	[$n, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = [];
	for ($i = 0; $i < $n; $i++) $A[] = array_map("intval", explode(' ', fgets(STDIN)));
	for ($i = 0; $i < $k; $i++) {
		for ($j = 0; $j < $n; $j++) {
			if ($j) echo ' ';
			echo $A[$j][$i];
		}
		echo PHP_EOL;
	}
?>
```
###### かけ算表
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	for ($i = 0; $i < $n; $i++) {
		for ($j = 0; $j < $n; $j++) {
			if ($j) echo ' ';
			echo $A[$i] * $A[$j];
		}
		echo PHP_EOL;
	}
?>
```
###### 素数の個数
```php
<?php
	$n = intval(fgets(STDIN));
	$cnt = 0;
	for ($k = 2; $k <= $n; $k++)
		for ($i = 2; $i <= $k; $i++)
			if ($i == $k) $cnt++;
			else if ($k % $i == 0) break;
	echo $cnt, PHP_EOL;
?>
```
###### log2
```php
<?php
	$n = intval(fgets(STDIN));
	$cnt = 0;
	for ($k = 2; $k <= $n; $k++)
		for ($i = 2; $k % $i == 0; $i *= 2)
			$cnt++;
	echo $cnt, PHP_EOL;
?>
```
- - -
##### Perl
###### 行列の転置
```pl
my ($n, $k) = map { int($_) } split ' ', <STDIN>;
my @A;
for (1..$n) {
	push @A, [map { int($_) } split ' ', <STDIN>];
}
for (my $i = 0; $i < $k; $i++) {
	for (my $j = 0; $j < $n; $j++) {
		if ($j) {
			print ' ';
		}
		print $A[$j][$i];
	}
	print $/;
}
```
###### かけ算表
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
for (my $i = 0; $i < $n; $i++) {
	for (my $j = 0; $j < $n; $j++) {
		if ($j) {
			print ' ';
		}
		print $A[$i] * $A[$j];
	}
	print $/;
}
```
###### 素数の個数
```pl
my $n = int(<STDIN>);
my $cnt = 0;
for (my $k = 2; $k <= $n; $k++) {
	for (my $i = 2; $i <= $k; $i++) {
		if ($i == $k) {
			$cnt++;
		} else {
			last if $k % $i == 0;
		}
	}
}
print "$cnt$/";
```
###### log2
```pl
my $n = int(<STDIN>);
my $cnt = 0;
for (my $k = 1; $k <= $n; $k++) {
	for (my $i = 2; $k % $i == 0; $i *= 2) {
		$cnt++;
	}
}
print "$cnt$/";
```
- - -
##### Python3
###### 行列の転置
```py
n, k = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
for i in range(k):
	T = []
	for j in range(n):
		T.append(A[j][i])
	print(*T)
```
###### かけ算表
```py
n = int(input())
A = list(map(int, input().split()))
for i in range(n):
	P = []
	for j in range(n):
		P.append(A[i] * A[j])
	print(*P)
```
###### 素数の個数
```py
n = int(input())
cnt = 0
for k in range(2, n + 1):
	for i in range(2, k):
		if k % i == 0:
			break
	else:
		cnt += 1
print(cnt)
```
###### log2
```py
n = int(input())
cnt = 0
for k in range(1, n + 1):
	i = 2
	while k % i == 0:
		cnt += 1
		i *= 2
print(cnt)
```
- - -
##### Ruby
###### 行列の転置
```rb
N, K = gets.split.map(&:to_i)
A = N.times.map { gets.split.map(&:to_i) }
K.times do |i|
	N.times do |j|
		if j > 0
			print ' '
		end
		print A[j][i]
	end
	puts
end
```
###### かけ算表
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
N.times do |i|
	N.times do |j|
		if j > 0
			print ' '
		end
		print A[i] * A[j]
	end
	puts
end
```
###### 素数の個数
```rb
N = gets.to_i
cnt = 0
(2..N).each do |k|
	(2..k).each do |i|
		if i == k
			cnt += 1
		elsif k % i == 0
			break
		end
	end
end
p cnt
```
###### log2
```rb
N = gets.to_i
cnt = 0
(1..N).each do |k|
	i = 2
	while k % i == 0
		cnt += 1
		i *= 2
	end
end
p cnt
```
- - -
##### Scala
###### 行列の転置
```scala
import scala.io.StdIn._

object Main extends App{
	val Array(n, k) = readLine().split(' ').map { _.toInt }
	val A = Array.fill(n){ readLine().split(' ').map { _.toInt } }
	for (i <- 0 until k) {
		for (j <- 0 until n) {
			if (j > 0) print(' ')
			print(A(j)(i))
		}
		println()
	}
}
```
###### かけ算表
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	for (i <- 0 until n) {
		for (j <- 0 until n) {
			if (j > 0) print(' ')
			print(A(i) * A(j))
		}
		println()
	}
}
```
###### 素数の個数
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	var cnt = 0
	for (k <- 2 to n) {
		var i = 2
		while (i < k && k % i != 0) i += 1
		if (i == k) cnt += 1
	}
	println(cnt)
}
```
###### log2
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	var cnt = 0
	for (k <- 1 to n) {
		var i = 2
		while (k % i == 0) {
			cnt += 1
			i *= 2
		}
	}
	println(cnt)
}
```
- - -
##### Swift
###### 行列の転置
```swift
let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k) = (nk[0], nk[1])
var A: [[Int]] = []
for _ in 0..<n {
	A.append(readLine()!.split(separator: " ").compactMap { Int($0) })
}
for i in 0..<k {
	var T: [Int] = []
	for j in 0..<n {
		T.append(A[j][i])
	}
	print(T.map { String($0) }.joined(separator: " "))
}
```
###### かけ算表
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
for i in 0..<n {
	var P: [Int] = []
	for j in 0..<n {
		P.append(A[i] * A[j])
	}
	print(P.map { String($0) }.joined(separator: " "))
}
```
###### 素数の個数
```swift
let n = Int(readLine()!)!
var cnt = 0
for k in 2...n {
	for i in 2...k {
		if i == k {
			cnt += 1
		} else if k % i == 0 {
			break
		}
	}
}
print(cnt)
```
###### log2
```swift
let n = Int(readLine()!)!
var cnt = 0
for k in 1...n {
	var i = 2
	while k % i == 0 {
		cnt += 1
		i *= 2
	}
}
print(cnt)
```