では、ここで様々な言語で「入力された数値以下の素数を返す」プログラムの例を紹介。
関数を作って、ループとIf文という基本構造を作っているので是非参考にしていただきたい。
実装した言語:HSP, C, C++, VB.net, C#, Python3, Ruby
諦めた言語:Bash, Perl
HSP - 文字列を表示すると勝手に改行される仕様との戦い
#runtime "hsp3cl"
#module
#defcfunc isPrime int x
if(x<2){
return 0
}
for i,2,int(sqrt(x)+1)
if(i!=x){
if(x\i==0){
return 0
}
}
next
return 1
#global
mes "自然数を入力してください"
input a,,2
a=int(a)
mes strf("%d以下の素数は以下のとおりです",a)
l="{ "
for i,2,a+1
if(isPrime(i)){
l+=strf("%d,",i)
}
next
l=strmid(l,0,strlen(l)-1)+" }"
mes l
C - 文字列周りが鬼のような高級アセンブル言語。ポインタもポンポン出てくる。
http://ideone.com/IkE74j
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(int x){
int i;
if(x<2){
return 0;
}
for(i=2;i<=(int)sqrt(x);i++){
if(i!=x){
if(x%i==0){
return 0;
}
}
}
return 1;
}
int main(void) {
int i, a;
char* l;//文字列型
int p=0,sp=4096;//lの文字列長と最大文字列長を記録しておく
printf("自然数を入力してください\n");
scanf("%d",&a);
printf("%d以下の素数は以下のとおりです\n",a);
l=(char*)malloc(sp);//ポインタで
p=sprintf(l,"{ ");
for(i=2;i<=a;i++){
if(isPrime(i)){
p=sprintf(l,"%s%d,",l,i);
if(p*2>sp){
sp*=2;
char* tmp=(char*)realloc(l,sp);
free(l);
l=tmp;
}
}
}
l[p-1]='\0';//最後の,をカット
sprintf(l,"%s }",l);
printf(l);
return 0;
}
C++(14) - モダンなC++は最強の言語だと思う。難易度も最強だ(と言われている)が私は好きだ。isPrime関数はCと同じであるようにCとの互換もある。
http://ideone.com/03EwRg
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int isPrime(int x){
int i;
if(x<2){
return 0;
}
for(i=2;i<=(int)sqrt(x);i++){
if(i!=x){
if(x%i==0){
return 0;
}
}
}
return 1;
}
int main(void) {
string l;//文字列型
cout<<"自然数を入力してください"<<endl;
int a;
cin>>a;
cout<<a<<"以下の素数は以下のとおりです"<<endl;
l="{ ";
for(int i=2;i<=a;i++){
if(isPrime(i)){
l+=to_string(i)+",";
}
}
l.pop_back();
l+=" }";
cout<<l<<endl;
return 0;
}
C# - C++++の意味を持つ言語。Visual Studioを使う人の過半数が利用する言語(だと思う)。Javaとガチバトルしてる。
http://ideone.com/fPJL1J
using System;
class Program
{
static bool isPrime(int x)
{
if (x < 2)
{
return false;
}
for (int i = 2; i <= (int)Math.Sqrt(x); i++)
{
if (i != x)
{
if (x % i == 0)
{
return false;
}
}
}
return true;
}
static void Main(string[] args)
{
Console.WriteLine("自然数を入力してください");
int a;
a = int.Parse(Console.ReadLine());//型変換が厳格である。
Console.WriteLine(a.ToString() + "以下の素数は以下のとおりです");
string l = "{ ";
for(int i = 2; i <= a; i++)
{
if (isPrime(i))
{
l += i.ToString() + ",";
}
}
l= l.Substring(0, l.Length - 1);
l += " }";
Console.WriteLine(l);
}
}
VB.net - HSPに最も近い有名高級言語。C#と同じようなコードを書くことになることにも注目。後読める(英語的に)
Module Module1
Function isPrime(ByVal x As Integer) As Boolean
If x < 2 Then
Return False
End If
For i As Integer = 2 To (Math.Sqrt(x))
If i <> x Then
If x Mod i = 0 Then
Return False
End If
End If
Next
Return True
End Function
Sub Main()
Console.WriteLine("自然数を入力してください")
Dim a As Integer
a = Integer.Parse(Console.ReadLine())
Console.WriteLine(a.ToString() + "以下の素数は以下のとおりです")
Dim l As String = "{ "
For i As Integer = 2 To a
If isPrime(i) Then
l += i.ToString() + ","
End If
Next
l = l.Substring(0, l.Length - 1)
l += " }"
Console.WriteLine(l)
End Sub
End Module
Python3 - 有名なLL言語(4P言語の1つ)。インデントでブロック構造を形成する。はよPython2をなんとかしろ(廃止しろ)
# your code goes here
import math
def isPrime(x):
if x<2:
return 0
for i in range(2,int(math.ceil(math.sqrt(x)))+1):
if i!=x:
if x%i==0:
return 0
return 1
print("自然数を入力してください")
a=int(input())
print(str(a)+"以下の素数は以下のとおりです")
l="{ "
for i in range(2,a+1):
if(isPrime(i)):
l+=str(i)+","
l=l[0:-1]+" }"
print(l)
Ruby - 日本製のLL言語(4P言語の1つ)。書き方が常に複数あるような思想。(Pythonは1つになるように作られている)
http://ideone.com/MFC9Sj
# your code goes here
def isPrime(x)
if(x<2) then
return 0
end
for i in 2..(Math.sqrt(x)).to_i do
if(i!=x) then
if(x%i==0) then
return 0
end
end
end
return 1
end
print("自然数を入力してください\n")
a = gets.chomp.to_i
print(a,"以下の素数は以下のとおりです\n")
l="{ "
for i in 2..a do
if(isPrime(i)==1) then
l<<i.to_s+","
end
end
l.slice!(-1)
l+=" }"
print(l)