Skip to main content
edited tags
Link
Wheat Wizard
  • 102.9k
  • 23
  • 299
  • 697
added 118 characters in body
Source Link

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


Challenge

Define a function f:Q->Q\$f:\mathbb{Q}\to\mathbb{Q}\$ such that f(f(n)) = -n\$f(f(n)) = -n\$ for all non-zero integers n\$n\$, and whereQ \$\mathbb{Q}\$ is the set of rational numbers.

Details

In whatever language you prefer, please define one function or program f\$f\$ that accepts as parameter one number n\$n\$ and returns or outputs one number f(n)\$f(n)\$.

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function f\$f\$. For example, keeping a counter outside of f\$f\$ that counts how many times f\$f\$ was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions f\$f\$ makes should rely only on data within f\$f\$'s lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


Scoring

CommonStandard code golf rules apply  - your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of f\$f\$ to be a subset of the rationals Q\$\mathbb{Q}\$. If you restrict your domain and codomain of f\$f\$ to the integers Z\$\mathbb{Z}\$, then your score is the ceiling of 90% of the number of bytes in your source code.

Tiebreak

In the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

Edit

You are not required to support arbitrarily sized numbers. Please interpret the sets Z\$\mathbb{Z}\$ and Q\$\mathbb{Q}\$ as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


Challenge

Define a function f:Q->Q such that f(f(n)) = -n for all non-zero integers n, and whereQ is the set of rational numbers.

Details

In whatever language you prefer, please define one function or program f that accepts as parameter one number n and returns or outputs one number f(n).

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function f. For example, keeping a counter outside of f that counts how many times f was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions f makes should rely only on data within f's lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


Scoring

Common code golf rules apply- your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of f to be a subset of the rationals Q. If you restrict your domain and codomain of f to the integers Z, then your score is the ceiling of 90% of the number of bytes in your source code.

Tiebreak

In the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

Edit

You are not required to support arbitrarily sized numbers. Please interpret the sets Z and Q as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


Challenge

Define a function \$f:\mathbb{Q}\to\mathbb{Q}\$ such that \$f(f(n)) = -n\$ for all non-zero integers \$n\$, and where \$\mathbb{Q}\$ is the set of rational numbers.

Details

In whatever language you prefer, please define one function or program \$f\$ that accepts as parameter one number \$n\$ and returns or outputs one number \$f(n)\$.

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function \$f\$. For example, keeping a counter outside of \$f\$ that counts how many times \$f\$ was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions \$f\$ makes should rely only on data within \$f\$'s lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


Scoring

Standard code golf rules apply  - your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of \$f\$ to be a subset of the rationals \$\mathbb{Q}\$. If you restrict your domain and codomain of \$f\$ to the integers \$\mathbb{Z}\$, then your score is the ceiling of 90% of the number of bytes in your source code.

Tiebreak

In the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

You are not required to support arbitrarily sized numbers. Please interpret the sets \$\mathbb{Z}\$ and \$\mathbb{Q}\$ as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

Commonmark migration
Source Link

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


Challenge

#Challenge# DefineDefine a function f:Q->Q such that f(f(n)) = -n for all non-zero integers n, and whereQ is the set of rational numbers.

Details

#Details# InIn whatever language you prefer, please define one function or program f that accepts as parameter one number n and returns or outputs one number f(n).

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function f. For example, keeping a counter outside of f that counts how many times f was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions f makes should rely only on data within f's lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


Scoring

#Scoring# CommonCommon code golf rules apply- your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of f to be a subset of the rationals Q. If you restrict your domain and codomain of f to the integers Z, then your score is the ceiling of 90% of the number of bytes in your source code.

Tiebreak

#Tiebreak# InIn the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

Edit

###Edit### YouYou are not required to support arbitrarily sized numbers. Please interpret the sets Z and Q as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


#Challenge# Define a function f:Q->Q such that f(f(n)) = -n for all non-zero integers n, and whereQ is the set of rational numbers.

#Details# In whatever language you prefer, please define one function or program f that accepts as parameter one number n and returns or outputs one number f(n).

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function f. For example, keeping a counter outside of f that counts how many times f was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions f makes should rely only on data within f's lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


#Scoring# Common code golf rules apply- your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of f to be a subset of the rationals Q. If you restrict your domain and codomain of f to the integers Z, then your score is the ceiling of 90% of the number of bytes in your source code.

#Tiebreak# In the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

###Edit### You are not required to support arbitrarily sized numbers. Please interpret the sets Z and Q as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle


Challenge

Define a function f:Q->Q such that f(f(n)) = -n for all non-zero integers n, and whereQ is the set of rational numbers.

Details

In whatever language you prefer, please define one function or program f that accepts as parameter one number n and returns or outputs one number f(n).

Input may be provided through whichever mechanism is most natural for your language: function argument, read from STDIN, command-line argument, stack position, voice input, gang signs, etc.

Output should be a return value from a function/program or printed to STDOUT.

I would like to restrict answers to functions that do not take advantage of program state or global memory/data that is visible from outside of the function f. For example, keeping a counter outside of f that counts how many times f was called and just doing a negation based on this count isn't very challenging or interesting for anyone. The decisions f makes should rely only on data within f's lexical scope.

However, this restriction is probably inappropriate for some stack-oriented languages or other types of languages that do not distinguish these types of data or scopes. Please use your best judgement to keep with the spirit of this challenge.


Scoring

Common code golf rules apply- your score is the number of bytes in your source code.

The minimal answer requires the domain and codomain of f to be a subset of the rationals Q. If you restrict your domain and codomain of f to the integers Z, then your score is the ceiling of 90% of the number of bytes in your source code.

Tiebreak

In the event of a tie, the following will be used in order:

  1. Fewest number of printable non-whitespace symbols in your source code
  2. Earliest date and time of answer submission

Edit

You are not required to support arbitrarily sized numbers. Please interpret the sets Z and Q as datatypes in your chosen language (typically integer and floating point, respectively).

If your solution relies entirely on the underlying structure or bit pattern of a data type, please describe its limitations and how it is being used.

Question Unprotected by Stewie Griffin
Question Protected by CommunityBot
Question Unprotected by CommunityBot
Question Protected by CommunityBot
Tweeted twitter.com/#!/StackCodeGolf/status/354302257522688001
clarified integer and rational domains
Source Link
ardnew
  • 2.3k
  • 1
  • 18
  • 24
Loading
Clarified bones points criteria
Source Link
ardnew
  • 2.3k
  • 1
  • 18
  • 24
Loading
Source Link
ardnew
  • 2.3k
  • 1
  • 18
  • 24
Loading