Skip to main content
Code Review

Return to Question

Title, tags, description
Source Link
Dan Oberlam
  • 8k
  • 2
  • 33
  • 74

How to count 1 − 2 + 3 − 4 + · · · + (−1)^(n)n with a better performance Efficiently calculate the value of an arithmetic sequence

Good day, I'm having a taskI need to calculate f(n) = -1 + 2 -3 + .. +(-1)^n.n the value for the following function:

$$f(n) = -1 + 2 - 3 + ... + -1^n$$

The time limit per test has to becalculate a given value of \$n\$ is less than 1 second, but unfortunately my approach failed to respect the timing. More exactly my code failed to count f(1000000000)My approach does not meet that requirement at the right timelarge sizes, for example \$f(1000000000)\$.

My code:

program A486;
uses wincrt, sysutils;
var
 n: LongInt;
function f(n : LongInt): LongInt;
var
 i : LongInt;
 //Result : LongInt;
begin
 Result:= 0;
 for i:= 1 to n do 
 if i mod 2 = 0 then
 Result += i
 else
 Result -= i;
 //f:= Result; 
end;
begin
 readln(n);
 writeln(f(n));
 //readkey()
end. 

I was wondering if I can getIs there a better idea. I apologize if I'm getting out of contest, asway I see most of the question here are more professional than educative, so if I'm choosing the wrong place to get my answer feel free to tell me about that. Thanks for replying and for everyone who shares knowledge!can do this?

How to count 1 − 2 + 3 − 4 + · · · + (−1)^(n)n with a better performance

Good day, I'm having a task to calculate f(n) = -1 + 2 -3 + .. +(-1)^n.n the time limit per test has to be less than 1 second, but unfortunately my approach failed to respect the timing. More exactly my code failed to count f(1000000000) at the right time.

My code:

program A486;
uses wincrt, sysutils;
var
 n: LongInt;
function f(n : LongInt): LongInt;
var
 i : LongInt;
 //Result : LongInt;
begin
 Result:= 0;
 for i:= 1 to n do 
 if i mod 2 = 0 then
 Result += i
 else
 Result -= i;
 //f:= Result; 
end;
begin
 readln(n);
 writeln(f(n));
 //readkey()
end. 

I was wondering if I can get a better idea. I apologize if I'm getting out of contest, as I see most of the question here are more professional than educative, so if I'm choosing the wrong place to get my answer feel free to tell me about that. Thanks for replying and for everyone who shares knowledge!

Efficiently calculate the value of an arithmetic sequence

I need to calculate the value for the following function:

$$f(n) = -1 + 2 - 3 + ... + -1^n$$

The time limit to calculate a given value of \$n\$ is less than 1 second. My approach does not meet that requirement at large sizes, for example \$f(1000000000)\$.

My code:

program A486;
uses wincrt, sysutils;
var
 n: LongInt;
function f(n : LongInt): LongInt;
var
 i : LongInt;
 //Result : LongInt;
begin
 Result:= 0;
 for i:= 1 to n do 
 if i mod 2 = 0 then
 Result += i
 else
 Result -= i;
 //f:= Result; 
end;
begin
 readln(n);
 writeln(f(n));
 //readkey()
end. 

Is there a better way I can do this?

Source Link

How to count 1 − 2 + 3 − 4 + · · · + (−1)^(n)n with a better performance

Good day, I'm having a task to calculate f(n) = -1 + 2 -3 + .. +(-1)^n.n the time limit per test has to be less than 1 second, but unfortunately my approach failed to respect the timing. More exactly my code failed to count f(1000000000) at the right time.

My code :

program A486;
uses wincrt, sysutils;
var
 n: LongInt;
function f(n : LongInt): LongInt;
var
 i : LongInt;
 //Result : LongInt;
begin
 Result:= 0;
 for i:= 1 to n do 
 if i mod 2 = 0 then
 Result += i
 else
 Result -= i;
 //f:= Result; 
end;
begin
 readln(n);
 writeln(f(n));
 //readkey()
end. 

I was wondering if I can get a better idea. I apologize if I'm getting out of contest, as I see most of the question here are more professional than educative, so if I'm choosing the wrong place to get my answer feel free to tell me about that. Thanks for replying and for everyone who shares knowledge!

default

AltStyle によって変換されたページ (->オリジナル) /