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ラーニングレベルアップ問題集の「約数の個数」「約数の列挙」をやってみました。


問題
約数の個数

約数の列挙


方針

$i=1,\dots,N$について、$N\%i=0$ならば$i$は$N$の約数です。


C
約数の個数
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int count = 0;
	for (int i = 1; i <= n; i++) if (n % i == 0) count++;
	printf("%d\n", count);
	return 0;
}
約数の列挙
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) if (n % i == 0) printf("%d\n", i);
	return 0;
}

C++
約数の個数
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int count = 0;
	for (int i = 1; i <= n; i++) if (n % i == 0) count++;
	cout << count << endl;
	return 0;
}
約数の列挙
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) if (n % i == 0) cout << i << endl;
	return 0;
}

C#
約数の個数
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int count = 0;
		for (int i = 1; i <= n; i++) if (n % i == 0) count++;
		Console.WriteLine(count);
	}
}
約数の列挙
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int count = 0;
		for (int i = 1; i <= n; i++) if (n % i == 0) Console.WriteLine(i);
	}
}

Go
約数の個数
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	count := 0
	for i := 1; i <= n; i++ {
		if n % i == 0 {
			count++
		}
	}
	fmt.Println(count)
}
約数の列挙
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		if n % i == 0 {
			fmt.Println(i)
		}
	}
}

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 count = 0;
		for (int i = 1; i <= n; i++) if (n % i == 0) count++;
		System.out.println(count);
	}
}
約数の列挙
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		for (int i = 1; i <= n; i++) if (n % i == 0) System.out.println(i);
	}
}

JavaScript
約数の個数
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
let count = 0;
for (var i = 1; i <= n; i++) if (n % i === 0) count++;
console.log(count);
約数の列挙
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
for (var i = 1; i <= n; i++) if (n % i === 0) console.log(i);

Kotlin
約数の個数
fun main() {
	val n = readLine()!!.toInt()
	var count = 0
	for (i in 1..n) if (n % i == 0) count++
	println(count)
}
約数の列挙
fun main() {
	val n = readLine()!!.toInt()
	for (i in 1..n) if (n % i == 0) println(i)
}

PHP
約数の個数
<?php
	$n = intval(fgets(STDIN));
	$count = 0;
	for ($i = 1; $i <= $n; $i++) if ($n % $i === 0) $count++;
	echo $count, PHP_EOL;
?>
約数の列挙
<?php
	$n = intval(fgets(STDIN));
	for ($i = 1; $i <= $n; $i++) if ($n % $i === 0) echo $i, PHP_EOL;
?>

Perl
約数の個数
my $n = int(<STDIN>);
my $count = 0;
for my $i (1..$n) {
	if ($n % $i == 0) {
		$count++;
	}
}
print "$count$/";
約数の列挙
my $n = int(<STDIN>);
for my $i (1..$n) {
	if ($n % $i == 0) {
		print "$i$/";
	}
}

Python3
約数の個数
N = int(input())
count = 0
for i in range(1, N + 1):
	if N % i == 0:
		count += 1
print(count)
約数の列挙
N = int(input())
for i in range(1, N + 1):
	if N % i == 0:
		print(i)

Ruby
約数の個数
N = gets.to_i
count = 0
(1..N).each do |i|
	if N % i == 0
		count += 1
	end
end
p count
約数の列挙
N = gets.to_i
(1..N).each do |i|
	if N % i == 0
		p i
	end
end

Scala
約数の個数
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	var count = 0
	for (i <- 1 to n) if (n % i == 0) count += 1
	println(count)
}
約数の列挙
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	for (i <- 1 to n) if (n % i == 0) println(i)
}

Swift
約数の個数
let n = Int(readLine()!)!
var count = 0
for i in 1...n {
	if n % i == 0 {
		count += 1
	}
}
print(count)
約数の列挙
let n = Int(readLine()!)!
for i in 1...n {
	if n % i == 0 {
		print(i)
	}
}
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?