3
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?

More than 5 years have passed since last update.

合成数列の和アドベントカレンダー Pascal 編

Last updated at Posted at 2018-12-02

合成数列の和アドベントカレンダー Advent Calendar 2018 の 2 日目です

ルール

  • 入力として正の整数 N を与えたら 4 から始まる 合成数 の数列の 1 番目から N 番目からの合計を出力してください
  • N は最大で 100 とします
test.p
program test(output);
var primeNumber,compositeCount,n,amax,num,sum: integer;
function IsPrime(n : integer) : boolean;
begin
	amax:=trunc(sqrt(n));
	if n mod 2=0 then
	begin 
		if n=2 then IsPrime := True
		else IsPrime := False;
	end
	else
	begin
		primeNumber:=3;
	while (n mod primeNumber<>0) and (primeNumber<=amax) do primeNumber:=primeNumber+2;
	if (n mod primeNumber=0) and (n>primeNumber) then IsPrime:=False
	else IsPrime:= True;
	end;
end;

begin
	read(n);
	num := 4;
	compositeCount := 0;
	while compositeCount < n do
	begin
		if not IsPrime(num) then
			begin
				compositeCount := compositeCount + 1;
				sum := sum + num
			end;
		num := num + 1;
	end;
	writeln(sum);
end.


#出力例

$ test //実行する
4 //Nの数を入力する
27 //結果

$ test //実行する
10 //Nの数を入力する
112 //結果

$ test //実行する
200 //Nの数を入力する
26558 //結果

メモ

この問題はずんだ問題の番外編です

3
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
3
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?