LoginSignup
0
0

More than 5 years have passed since last update.

合成数列の和 (Pascal)

Last updated at Posted at 2018-12-12
合成数列の和 https://qiita.com/advent-calendar/2018/composite-number
Python(/Ruby) https://qiita.com/cielavenir/items/dcbd839b0c66af1bc971
Crystal https://qiita.com/cielavenir/items/1a0dd1a94aca9935f5aa
Kuin https://qiita.com/cielavenir/items/47fd18fc68055d990599
Swift https://qiita.com/cielavenir/items/6cde3b20866c842dc9e2
Kotlin https://qiita.com/cielavenir/items/f58eaa30f90781229a6d
Pascal https://qiita.com/cielavenir/items/35c2fc54ca16b620c9c7
composite.pas
program composite;
var i,s,n,cnt: longint;

function isqrt(n: longint): longint;
var
    x: longint;
    y: longint;
begin
    if(n<=0) then
        isqrt:=0
    else if(n<4) then
        isqrt:=1
    else begin
        x:=0;
        y:=n;
        while((x<>y) and (x+1<>y)) do begin
            x:=y;
            y:=(n div y+y) div 2;
        end;
        isqrt:=x;
    end;
end;

function isPrime(i: longint): boolean;
var
    j: longint;
begin
    isPrime:=true;
    if i<2 then isPrime:=false;
    for j:=2 to isqrt(i) do begin
        if(i mod j < 1) then
            isPrime:=false;
    end;
end;

begin
    read(n);
    i:=2;
    cnt:=0;
    s:=0;
    while(cnt<n) do begin
        if(not isPrime(i)) then begin
            s:=s + i;
            cnt:=cnt + 1;
        end;
        i:=i + 1;
    end;
    writeln(s);
end.
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