Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Reduce over the range [1..n]

C (clang), 59 bytes c;f(int a,(*b)(int,int)){for(c=a;a>1;)c=b(--a,c);return c;} Try it online! This program written by @Hakerh400 in the comments is basically how the other answers before...

posted 2y ago by General Sebast1an‭  ·  edited 2y ago by General Sebast1an‭

Answer
#5: Post edited by user avatar General Sebast1an‭ · 2021-08-11T14:16:15Z (over 2 years ago)
  • # [C (clang)], 59 bytes
  • <!-- language-all: lang-c -->
  • c;f(int a,(*b)(int,int)){for(c=a;a>1;)c=b(--a,c);return c;}
  • [Try it online!][TIO-ks7earpp]
  • This program written by [**@Hakerh400**](https://codegolf.codidact.com/users/53579) in the comments basically how the other answers before this post existed actually did it, by a reduction sort of method. I'd like to thank them for making such program and the amount of bytes it literally shaved compared to the program below.
  • ---------
  • # [C (clang)], <s>142</s> 103 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==42){j*=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks7e7ke0]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks7e7ke0]: https://tio.run/##TYtBDoIwFAXXcAqCwbRSjSgb86lHISmF6idYTIEEQnr2Wl2ouzeTeXIvO6FvboNadlPdRMUw1tgf7leHrAVFBKvoiopUnOcnurY8A6t6Q9APLLgATNNvcPZByhHs32H340vuuXyzNc04GR21YN0CD4GazHQNg0EKrUicYCJjtp3ZdqEQBk@DevzomCkys4V6a112LF8 "C (clang) – Try It Online"
  • [TIO-ks7earpp]: https://tio.run/##fc7RCoIwFAbge5/iYAQ7NqMCr5Y@SnC2uRJ0hikI4rOviXVT6sXgsI//nF/FqiR7d7vCqrLTOVxfrS7q4yNzShhW2BaIs0jiNHL/EAdTN0ylJCg7C1SpZHFMXKFo8rZrLCgxOtKaEQeJA3x@CQ4gBYxB1ZV/FM3U@8W/dJspqKiwrMchAHg2vodh4V5DyMGwMwfwBxHFAiZbOCV9n/XkKk5JX3cZL1uYLOO89cS/Nro3 "C (clang) – Try It Online"
  • # [C (clang)], 59 bytes
  • <!-- language-all: lang-c -->
  • c;f(int a,(*b)(int,int)){for(c=a;a>1;)c=b(--a,c);return c;}
  • [Try it online!][TIO-ks7earpp]
  • This program written by [**@Hakerh400**](https://codegolf.codidact.com/users/53579) in the comments is basically how the other answers before this post existed actually did it, by a reduction sort of method. I'd like to thank them for making such program and the amount of bytes it literally shaved compared to the program below.
  • ---------
  • # [C (clang)], <s>142</s> 103 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==42){j*=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks7e7ke0]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks7e7ke0]: https://tio.run/##TYtBDoIwFAXXcAqCwbRSjSgb86lHISmF6idYTIEEQnr2Wl2ouzeTeXIvO6FvboNadlPdRMUw1tgf7leHrAVFBKvoiopUnOcnurY8A6t6Q9APLLgATNNvcPZByhHs32H340vuuXyzNc04GR21YN0CD4GazHQNg0EKrUicYCJjtp3ZdqEQBk@DevzomCkys4V6a112LF8 "C (clang) – Try It Online"
  • [TIO-ks7earpp]: https://tio.run/##fc7RCoIwFAbge5/iYAQ7NqMCr5Y@SnC2uRJ0hikI4rOviXVT6sXgsI//nF/FqiR7d7vCqrLTOVxfrS7q4yNzShhW2BaIs0jiNHL/EAdTN0ylJCg7C1SpZHFMXKFo8rZrLCgxOtKaEQeJA3x@CQ4gBYxB1ZV/FM3U@8W/dJspqKiwrMchAHg2vodh4V5DyMGwMwfwBxHFAiZbOCV9n/XkKk5JX3cZL1uYLOO89cS/Nro3 "C (clang) – Try It Online"
#4: Post edited by user avatar General Sebast1an‭ · 2021-08-11T12:02:14Z (over 2 years ago)
  • # [C (clang)], 142 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks75bste]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 45){result -= x;} // Subtraction
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 47){result /= x;} // Division
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • **Question**: Should I delete the division part? It seems useless.
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"
  • # [C (clang)], 59 bytes
  • <!-- language-all: lang-c -->
  • c;f(int a,(*b)(int,int)){for(c=a;a>1;)c=b(--a,c);return c;}
  • [Try it online!][TIO-ks7earpp]
  • This program written by [**@Hakerh400**](https://codegolf.codidact.com/users/53579) in the comments basically how the other answers before this post existed actually did it, by a reduction sort of method. I'd like to thank them for making such program and the amount of bytes it literally shaved compared to the program below.
  • ---------
  • # [C (clang)], <s>142</s> 103 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==42){j*=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks7e7ke0]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks7e7ke0]: https://tio.run/##TYtBDoIwFAXXcAqCwbRSjSgb86lHISmF6idYTIEEQnr2Wl2ouzeTeXIvO6FvboNadlPdRMUw1tgf7leHrAVFBKvoiopUnOcnurY8A6t6Q9APLLgATNNvcPZByhHs32H340vuuXyzNc04GR21YN0CD4GazHQNg0EKrUicYCJjtp3ZdqEQBk@DevzomCkys4V6a112LF8 "C (clang) – Try It Online"
  • [TIO-ks7earpp]: https://tio.run/##fc7RCoIwFAbge5/iYAQ7NqMCr5Y@SnC2uRJ0hikI4rOviXVT6sXgsI//nF/FqiR7d7vCqrLTOVxfrS7q4yNzShhW2BaIs0jiNHL/EAdTN0ylJCg7C1SpZHFMXKFo8rZrLCgxOtKaEQeJA3x@CQ4gBYxB1ZV/FM3U@8W/dJspqKiwrMchAHg2vodh4V5DyMGwMwfwBxHFAiZbOCV9n/XkKk5JX3cZL1uYLOO89cS/Nro3 "C (clang) – Try It Online"
#3: Post edited by user avatar General Sebast1an‭ · 2021-08-11T07:18:53Z (over 2 years ago)
  • # [C (clang)], 142 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks75bste]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 45){result -= x;} // Subtraction
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 47){result /= x;} // Division
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"
  • # [C (clang)], 142 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks75bste]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 45){result -= x;} // Subtraction
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 47){result /= x;} // Division
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • **Question**: Should I delete the division part? It seems useless.
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"
#2: Post edited by user avatar General Sebast1an‭ · 2021-08-11T07:17:06Z (over 2 years ago)
  • # [C (clang)], 142 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks75bste]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(b==43){result+=x;} // Addition
  • if(b==45){result-=x;} // Subtraction
  • if(b==42){result*=x;} // Multiplication
  • if(b==47){result/=x;} // Division
  • if(b==94){result^=x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"
  • # [C (clang)], 142 bytes
  • <!-- language-all: lang-c -->
  • i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}
  • [Try it online!][TIO-ks75bste]
  • I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.
  • Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.
  • Ungolfed form of the function with comments:
  • ```c
  • int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
  • int x, result = 0; // Assign 2 integers: the succeeding number and the base
  • if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
  • for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
  • if(operator == 43){result += x;} // Addition
  • if(operator == 45){result -= x;} // Subtraction
  • if(operator == 42){result *= x;} // Multiplication
  • if(operator == 47){result /= x;} // Division
  • if(operator == 94){result ^= x;} // XOR
  • }
  • return result; // Return the result
  • }
  • ```
  • [C (clang)]: http://clang.llvm.org/
  • [TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"
#1: Initial revision by user avatar General Sebast1an‭ · 2021-08-11T07:15:59Z (over 2 years ago)
# [C (clang)], 142 bytes

<!-- language-all: lang-c -->

    i,j;f(a,b){if(b==42||b==47){j=1;}for(i=1;i<=a;i++){if(b==43){j+=i;}if(b==45){j-=i;}if(b==42){j*=i;}if(b==47){j/=i;}if(b==94){j^=i;}}return j;}

[Try it online!][TIO-ks75bste]

I wanted to try to make something similar to [this](https://codegolf.codidact.com/posts/283268/283277#answer-283277), but with whatever Python has that C doesn't, I couldn't do so.

Instead, I chose to make a function that implements 5 operations. It works differently from the others, but it's all I could do.

Ungolfed form of the function with comments:

```c
int f(int num, char operator){ // Assign a function with 2 parameters: a number and character for operations
	int x, result = 0; // Assign 2 integers: the succeeding number and the base
	if(operator == '*' || operator == '/'){result = 1;} // Check if the character resembles multiplication or division; if so, set base as 1
	for(x = 1; x <= num; x++){ // Start a loop to prompt the calculation that satisfies the challenge
		if(b==43){result+=x;} // Addition
		if(b==45){result-=x;} // Subtraction
		if(b==42){result*=x;} // Multiplication
		if(b==47){result/=x;} // Division
		if(b==94){result^=x;} // XOR
	}
	return result; // Return the result
}
```

[C (clang)]: http://clang.llvm.org/
[TIO-ks75bste]: https://tio.run/##TczhCoIwFAXg3/UUYihbzkorIuZ6FGFOV1dsxVJQdM9uM6L8dc93OFwRioqr67gCJaomL5zkVefw2NwuI5CSSsRJhnuQKGPsEA/DdE64L1lEjXxoBDZAwjiFIPjt9nYQMKDm66N1OHNsvZ55erj9@3ywTicbXdSNVk5JzdjROweFWtwvFy/BlUSuB55wid8Sv8N0uXhqUPWndolELemwbc0Y7dI3 "C (clang) – Try It Online"