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

Comments on Reduce over the range [1..n]

Parent

Reduce over the range [1..n]

+7
−0

Task

I often need to find the factorial of a number or the sum of all numbers up to a number when cheating on math tests. To help me with this, your task is to write $F$, a generalized version of those functions:

$$F(n) = 1 * 2 * \space ... \space * (n-1) * n$$

Please note that the operator $ * $ does not necessarily represent multiplication here, but stands for a commutative, associative operator that will be an input to your program/function. This means that $a * b$ is the same as $b * a$, and $a * (b * c)$ is the same as $(a * b) * c$. Its inputs are positive integers, and its outputs are integers.

Rules

  • $n$ will be a positive integer.
  • $*$ is a binary function/operator that can be taken in any convenient format, including but not limited to:
    • A function object
    • A function pointer
    • An object with a method with a specific name (e.g. Java's BiFunction)
    • A string that can be evaluated to get a function
  • $*$ is a blackbox function. That means that you will not be able to examine it to see how it works; all you can do is feed it two positive integers and get an integer back.
  • The output of your function will be an integer (not necessarily positive).
  • This is code golf, so shortest code in bytes wins!

Testcases

f         | n  | F(f, n)
Add       | 1  | 1
Add       | 5  | 15
Multiply  | 1  | 1
Multiply  | 5  | 120
XOR       | 1  | 1
XOR       | 2  | 3
XOR       | 5  | 1
XOR       | 10 | 11
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Function constraint (2 comments)
Post
+1
−0

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 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), 142 103 bytes

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!

I wanted to try to make something similar to this, 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:

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
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Subtraction and division (1 comment)
Function instead of an operator (1 comment)
Function instead of an operator
Hakerh400‭ wrote about 3 years ago

In case you didn't know, C can take a function as an argument: 59 bytes