Skip to main content
added 9 characters in body
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13

C, C++, 46 40 37 bytes ( #define ), 50 4747 46 bytes ( function )

-1 byte thanks to Zacharý

-1011 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*~n*~n/4+n*~n*(n-~n)/6

Function version :

int f(int n){return n*n*~n*~nreturn~n*n*n*~n/4+n*~n*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 40 37 bytes ( #define ), 50 47 bytes ( function )

-1 byte thanks to Zacharý

-10 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*~n*~n/4+n*~n*(n-~n)/6

Function version :

int f(int n){return n*n*~n*~n/4+n*~n*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 40 37 bytes ( #define ), 50 47 46 bytes ( function )

-1 byte thanks to Zacharý

-11 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*~n*~n/4+n*~n*(n-~n)/6

Function version :

int f(int n){return~n*n*n*~n/4+n*~n*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

added 8 characters in body
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13

C, C++, 46 4040 37 bytes ( #define ), 5050 47 bytes ( function )

-1 byte thanks to Zacharý

-710 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*-~n*-~nn*n*~n*~n/4-n*-~n*4+n*~n*(n-~n)/6

Function version :

int f(int n){int p=n*n+n;returnreturn p*pn*n*~n*~n/4-p*4+n*~n*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 40 bytes ( #define ), 50 bytes ( function )

-1 byte thanks to Zacharý

-7 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*-~n*-~n/4-n*-~n*(n-~n)/6

Function version :

int f(int n){int p=n*n+n;return p*p/4-p*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 40 37 bytes ( #define ), 50 47 bytes ( function )

-1 byte thanks to Zacharý

-10 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*~n*~n/4+n*~n*(n-~n)/6

Function version :

int f(int n){return n*n*~n*~n/4+n*~n*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

added 4 characters in body
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13

C, C++, 4646 40 bytes ( #define ), 50 bytes ( function )

-1 byte thanks to Zacharý

-17 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*(n+1)*(n+1)-~n*-~n/4-n*(n+1)*-~n*(n-~n)/6

Function version :

int f(int n){int p=n*n+n;return p*p/4-p*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 bytes ( #define ), 50 bytes ( function )

-1 byte thanks to Zacharý

-1 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*(n+1)*(n+1)/4-n*(n+1)*(n-~n)/6

Function version :

int f(int n){int p=n*n+n;return p*p/4-p*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

C, C++, 46 40 bytes ( #define ), 50 bytes ( function )

-1 byte thanks to Zacharý

-7 bytes thanks to ceilingcat

Macro version :

#define F(n)n*n*-~n*-~n/4-n*-~n*(n-~n)/6

Function version :

int f(int n){int p=n*n+n;return p*p/4-p*(n-~n)/6;}

Thoses lines are based on thoses 2 formulas :

Sum of numbers between 1 and n = n*(n+1)/2
Sum of squares between 1 and n = n*(n+1)*(2n+1)/6

So the formula to get the answer is simply (n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6

And now to "optimize" the byte count, we break parenthesis and move stuff around, while testing it always gives the same result

(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6 => n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6 => n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6

Notice the pattern p = n*n+1 = n*n+n, so in the function, we declare another variable int p = n*n+n and it gives :

p*p/4 - p*(2n+1)/6

For p*(p/4-(2*n+1)/6) and so n*(n+1)*(n*(n+1)/4 - (2n+1)/6), it works half the time only, and I suspect integer division to be the cause ( f(3) giving 24 instead of 22, f(24) giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.

Both the macro and function version are here because of macro substitution :

F(3) gives 3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22
F(5-2) gives 5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30

and mess up with the operator precedence. the function version does not have this problem

added 31 characters in body
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13
Loading
added 28 characters in body
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13
Loading
Source Link
HatsuPointerKun
  • 2.2k
  • 1
  • 8
  • 13
Loading