Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 2 characters in body
Source Link
emanresu A
  • 46.2k
  • 5
  • 111
  • 257

#C++17, (削除) 82 (削除ここまで) 66 bytes

C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;

C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;
added 423 characters in body
Source Link
Karl Napf
  • 4.5k
  • 14
  • 31

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

#C++17, (削除) 82 (削除ここまで) 66 bytes

int f(){return 0;}int f(int H,auto... L){return(H*...*L)+f(L...);}

Uses the C++17 template parameter fold expression and essentially the same idea as Dennis. Saving 16 bytes by using Generic Variadic Lambda.

Explanation:

int f(){return 0;} //base case for empty list
 
int f(int H, auto... L) { //first element, variadic arguments
 return (H*...*L) //a_0*a_1*a_2*...
 + f(L...); //+ f(a_1,a_2,...)
}

Usage:

f(1,1,0,1,1,0,1,1,1,1,1) -> 5
f() -> 0
f(1,0,1,0) -> 0

##Non competing

Albeit longer, this also works with template constants:

template <int...L> int C=0;
template <int H, int...L> int C<H,L...> = (H*...*L)+C<L...>;

Usage:

std::cout << C<1,0,1,1> << std::endl;
std::cout << C<1,0,1,0,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1> << std::endl;
std::cout << C<1,1,1,0,1,0,1,1,1,1,1,1> << std::endl;
std::cout << C<> << std::endl;
added 15 characters in body
Source Link
Karl Napf
  • 4.5k
  • 14
  • 31
Loading
added 277 characters in body
Source Link
Karl Napf
  • 4.5k
  • 14
  • 31
Loading
Source Link
Karl Napf
  • 4.5k
  • 14
  • 31
Loading

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