LoginSignup
1
0

More than 5 years have passed since last update.

Fortran で Quine

Last updated at Posted at 2014-08-12

Fortran での自己出力プログラム

Fortran でも自己出力プログラムは昔から色々なものがあるので、少し毛色を変えてみようと思いました。自己出力プログラムでは、出力の反復が必要になりますが、それをFORMAT 文の繰り返しによって実現しています。

しかし、いまいちアイデアが無かったので、インチキ臭い手法を使いました。整数を Fortmat で文字で出力させる事自体は問題ないと思うのですが、8バイト長整数を使ってコンパイラに判断させています。また一行の長さも Fortran の規約を破っています。

Intel Fortran v.14 でコンパイルできています。

プログラム

print"(a,2(a6'7000883504211391088,43114511741484,48525234743591,1684956475')a)",7000883504211391088,43114511741484,48525234743591,1684956475;end

出力

print"(a,2(a6'7000883504211391088,43114511741484,48525234743591,1684956475')a)",7000883504211391088,43114511741484,48525234743591,1684956475;end
続行するには何かキーを押してください . . .

解説

それぞれの整数を文字として解釈して出力させると以下のようになります。

プログラム

    program self
      implicit none
      print "(a8)", 7000883504211391088 ! print"(a
      print "(a6)", 43114511741484      ! ,2(a6'
      print "(a6)", 48525234743591      ! ')a)",
      print "(a4)", 1684956475          ! ;end  
      stop
    end program self

出力 

print"(a
,2(a6'
')a)",
;end
続行するには何かキーを押してください . . .

はじめの二つが頭部のPRINT命令、次がフォーマットとデータの境界の記号、残りが尾部のEND文に対応しています。

1
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
1
0