[Question]
  [
Your task here will be to implement a function1 that forms a permutation on the positive integers (A bijection from the positive integers onto themselves). This means that each positive integer should appear exactly once in the permutation. The catch is your function should have a larger probability of outputting an odd number than an even number.
Now this may seem strange or impossible. Surely there are just as many odd numbers as even numbers? And while this intuition is correct for finite sets it actually does not hold for infinite sets. For example take the following permutation:
```
1 3 2 5 7 4 9 11 6 13 15 8 17 19 10 21 23 12 25 27 14 29 31 16 33 35 18 37 39 20 41 43 22 45 47 24 49 51 26 53 55 ...
```
If you take any subsection of the sequence with size greater than \$1\$ you will have at least as many odd numbers as even numbers, thus it seems that the probability of any random term being odd is greater than that of being even. You will also note every number odd or even number will eventually appear in the sequence and can only appear once. Thus the sequence is a true permutation.
### Definition of Probability
To avoid confusion or ambiguity I am going to clearly lay out what is meant by probability in this question.
Let us say we have a function \$f\$. The probability of a number being odd will be defined as the limit of ratio odd members of the set to the size of the set \$f\{1\dots n\}\$ as \$n\$ tends towards infinity.
$$\lim\_{n\rightarrow \infty}\dfrac{\left|\{x : x \in \{1\dots n\}, \mathrm{odd}(f(x))\}\right|}{n}$$
For example the aforementioned function would have a probability of being odd of \$2/3\$.
---
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so answers will be scored in bytes with less bytes being better.
---
### Extra Challenges
Here are some fun ideas to play around with and perhaps try to implement. These are just for fun and do not affect scoring in any way. Some of these are not even valid solutions to this challenge, and **an answer which only includes solutions to challenges 2 or 3 is not a valid answer, and [is liable to be deleted](https://codegolf.meta.stackexchange.com/a/7990/194)**.
* Write a permutation with an odd probability of \$1\$. (this is possible)
* Write a permutation that has more odd numbers than even numbers in \$f\{1\dots n\}\$ for any \$n\$ but has a odd probability of \$1/2\$.
* Write a permutation that has no defined probability (that is there is no limit). ([Now a standalone challenge here](https://codegolf.stackexchange.com/questions/200919))
---
1: Here function will mean program or function. It is just a piece of code that takes input and produces output.
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
Æf^<¥4P
```
Swaps **2**s and **3**s in the input's prime factorization. The probability of odds is **2/3**.
[Try it online!](https://tio.run/##y0rNyan8//9wW1qczaGlJgH/DQ0Mgg5tPbrncPujpjXu/wE "Jelly – Try It Online")
### How it works
```
Æf^<¥4P  Main link. Argument: n
Æf       Compute all prime factors of n, with duplicates.
    ¥4   Combine the two links to the left into a dyadic chain and call it with
         right argument 4.
   <       Compare each prime factor with 4. Yields 1 for 2 and 3, 0 otherwise.
  ^        Bitwise XOR the results with the corresponding prime factors.
         This maps 2 to 3, 3 to 2, and all other primes to themselves.
      P  Take the product of the resulting primes.
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~11~~ 10 bytes
-1 byte thanks to Leo, and a slightly different function
This has an odd probability of `1`
```
!uΣz:NCNİ1
```
[Try it online!](https://tio.run/##yygtzv6vkFuce3jboeX6PsqqRrmPmhof7lhcdGjbf8XSc4urrPyc/Y5sMPz//78hlxGXMZehAZeRAZepAZABxhDCAAA "Husk – Try It Online")
It indexes the sequence:
```
[1,2,3,5,7,9,11,4,13,15,17,19,21,23,25,27,29,6,31,33]
1 odd, 1 even, 5 odd, 1 even, 9 odd, 1 even, 13 odd...
```
### Explanation
```
!               Index the following sequence (1-indexed)
 u              remove duplicates                     [1,2,3,5,7,9,11,4,13,15...]
  Σ              Concatenate                          [1,1,2,3,5,3,7,9,11,4,13..]
   z:            Zipwith append                       [[1,1],[2,3,5],[3,7,9,11]..
     N          Natural numbers
      CNİ1      Odd numbers cut into lists of lengths [[1],[3,5],[7,9,11]...]
                corresponding to the Natural numbers
```
[Answer]
## Haskell, ~~35~~ ~~34~~ 32 bytes
```
f n=n:2*n+1:2*n+3:f(n+2)
(f 0!!)
```
Implements the example sequence `[1,3,2,5,7,4,9,11,6,13,15,8,17,19,10,21,...]`.
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hzzbPykgrT9sQTBpbpWnkaRtpcqXbaqQpGCgqav7PTczMU7BVKCjKzCtRUFHITSxQSFeINtTTMzaI/Q8A "Haskell – Try It Online")
For reference: old version, 34 bytes (-1 byte thanks to @xnor):
```
(!!)$do e<-[0,2..];[e,2*e+1,2*e+3]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/v9hWQ1FRUyUlXyHVRjfaQMdITy/WOjpVx0grVdsQTBrH/s9NzMxTsFUoKMrMK1FQUchNLFAoVog21NMzMYj9DwA "Haskell – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 8 bytes
```
!uΣzeİ1N
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8eGOxf8VS88trko9ssHQ7////yZGAA "Husk – Try It Online")
This implements the example sequence (`1,3,2,5,7,4...`).
### Explanation
```
!uΣzeİ1N
   ze       zip together
     İ1       the odd numbers
       N      with the natural (positive) numbers
  Σ         flatten the resulting list
 u          remove duplicates
!           index into the obtained sequence with the input
```
[Answer]
Everybody does Challenge 1, so let's do the other two.
# [Perl 6](https://perl6.org), 26 bytes — Challenge 2
```
{($_==1)+$_-(-1)**($_%%2)}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJKmYKvwv1pDJd7W1lBTWyVeV0PXUFNLCyigqmqkWfvfmis3sUCjWiUNKKJZq6NgqKdnaGCgqVecWGn9HwA "Perl 6 – Try It Online")
It's just `1 3 2 5 4 7 6...` In an even number of terms, there are always 2 more odd numbers than even. In an odd number, 1 more. However this has clearly limit of `(n+2)/(2n+2) -> ½`.
---
# [Perl 6](https://perl6.org), 70 bytes — Challenge 3
```
{((1,),(2,4),->@a,@ {@(map(@a[*-1]+2*(*+1),^(4*@a)))}...*).flat[$_-1]}
```
[Try it online!](https://tio.run/##FcsxDsIwDAXQnVNkyODvulZdVSwIlHtUUHkgUytVwFJFOXuA/b39@VrPbTtCzOEaWiEygdAoE6S/JZcUSqLNd0o@c2/3bmTiziAPmjg5gKqqDM2rf@a4/EhtlxOZqg0D9F9LzBQXVOjbj/YF "Perl 6 – Try It Online")
Admittedly, this is horribly golfed. It indexes a sequence that contains 2⁰ odd numbers, then 2¹ even, then 2² odd, then 2³ even, and so on.
The probability after n such "blocks", if n is odd, is (2⁰+2²+2⁴+...+2ⁿ⁻¹)/(2ⁿ-1). The sum in the numerator is equal to ⅓(4½(n+1) - 1) = ⅓(2n+1 - 1). So the probability after odd number of blocks is ⅔ (in the limit).
If we add one more block (and strike an even count of them n+1), however, we didn't add any odd numbers (numerator stays the same) but there is now (2n+1 - 1) numbers in total. The parentheses cancel and we get the probability of ⅓ (in the limit).
This is apparently supposed to have 2 different cluster points, ⅓ and ⅔, to make sure that the limit doesn't exist, but this doesn't really prove it. My attempt to do a solid, rigorous proof can be found in this Math.SE answer: <https://math.stackexchange.com/a/2416990/174637>. Bashing mistakes is welcome.
---
# [Perl 6](https://perl6.org), 39 bytes — The core challenge.
```
{my$l=$_ div 3;$_%3??2*($_-$l)-1!!2*$l}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJKmYKvwvzq3UiXHViVeISWzTMHYWiVe1dje3khLQyVeVyVHU9dQUdFISyWn9r81V25igUa1ShpQRrNWR8FQT8/QwEBTrzix0vo/AA "Perl 6 – Try It Online")
Though I posted this answer because of the challenges 2 and 3 which offered a pleasant little mathy puzzle, there is a strict requirement for all answers to contain a solution to the core challenge. Here it is then.
This is the example sequence.
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 120 bytes
```
(({})<{{({}[()]<({}(()[{}]))>)}{}({}[({})]<({}<>{}<({}())>)><>)}>)<>({}[()]){{}((<>{}<>[{}]){}[()])<>}{}{(({}){})<>{}}<>
```
[Try it online!](https://tio.run/##LY2xCsBACEN/xwyduoo/ctxwHQqlpUNXuW@3UW6QoHmJxzeudzufcUeI@IS6U5qgK1UEzWcHDJNbGmTKUuMkkqYpAYPaysIzW4hVwTqrscbrUf6iTyBi/wE "Brain-Flak – Try It Online")
Performs the following function:
[![function](https://i.stack.imgur.com/X853Q.png)](https://i.stack.imgur.com/X853Q.png)
This function generates the sequence
```
2 4 1 6 3 5 7 8 9 11 13 15 17 19 21 10 23 25 27 29...
```
The function has an odd probability of `1`
[Answer]
# R, 82 bytes (Extra challenge 1)
```
f<-function(n){if(sqrt(n)==floor(sqrt(n))){2*sqrt(n)}else{2*(n-floor(sqrt(n)))-1}}
```
[Try it Online!](https://tio.run/##XYoxDoAgDAB3XsHYmjDgZIy8xtCkkRQFnAhvRwZd3O4ul/qxmewvsKtdsNNm6Ja9cBQQrEyQr1QGOkchxvQpYp2nl5sP2Q8FMb/H2NY6jcKaRR@oq9JnYilAwIiq9Qc)
If the input is a perfect square, gives an even number. Otherwise, gives an odd number. The perfect squares have natural density 0, which means that this sequence gives odd numbers with probability 1.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 29 bytes
```
f(n){return n&3?n+n/2|1:n/2;}
```
[Try it online!](https://tio.run/##NY/NDoIwEITP9CkmGEgbkD9vAvoiXLRQbQKLQeCCPDu2Bvewm5nZL9mVx4eU26Y4iWVoxmkgkH@6UkBx9knPpufrdtAk26luULzHWvfR88KYphHdTRMXWJhjlQzRoUSa2MqZo/oB3AbaurkZBTozguCH/JjZRIprkVutwGf4SAUkAovspgFLZInAazCM4q5Xww0xW2plzt@tyIsyVZGJeN1P97YREjE6u7a/Zs5a2fYF "C (gcc) – Try It Online")
Every fourth number is even:
```
1 3 5   7 9 11   13 15 17   19 21 23   25 27 29
      2        4          6          8          10
```
# Extra challenge 1, 52 bytes
```
f(n,i){for(i=0;n>>i/2;i+=2);return n&n-1?2*n-i-1:i;}
```
[Try it online!](https://tio.run/##NY/BDoIwEETP9CsmGk0roFBvVvBHvGihuoksBoGL4duxNbqXyc7sy@7a9GbtPDvJCam3aztJRWa4LGmnDcWFVqar@6Fj8JrT/KQ3nFKaH8hM85LYPoaqxvHVV9Ru76UQxD2aC7FUeIsodDZBgwJ5FsqIyO@ADAEF13g5ovESx1/ky4w@cpKUCb2DHLFGrmARB@RnerDAXis8O884uVhVWCQYAzWJ6O@eebXV7sw@klU7XB@1stihCWO/1/xZk5g/ "C (gcc) – Try It Online")
Returns 2\*(x+1) if n equals 2x and consecutive odd numbers otherwise:
```
    1   3 5 7   9 11 13 15 17 19 21    23 25
2 4   6       8                     10      
```
[Answer]
# [Brain-Flak](https://github.com/Flakheads/BrainHack), ~~140~~ ~~138~~ 136 bytes
```
({}<(((()())()()))((<>[()])[()()])>){({}[()]<(({}(({}({}))[({}[{}])]))[({}[{}])]<>(({}(({}({}))[({}[{}])]))[({}[{}])])<>)>)}{}({}<{}{}>)
```
[Try it online!](https://tio.run/##jYw7DoBACESvw5R2FoSLbChWG42JhS3h7Di7laUkw2fmhe3p5330/aqSSBUWBJgNImpN4GjjdhiC0HAIRk5FgjHdSCfy2dV@QFDj25yMBqehqpb1BQ "Brain-Flak (BrainHack) – Try It Online")
## Explanation
This performs a similar function to the one suggested in the question.
```
2 3 1 4 7 5 6 11 9 8 15 13 10 17 15 ...
```
It works mostly based on a snippet I made to roll the stack for size 3 stacks.
```
(({}(({}({}))[({}[{}])]))[({}[{}])])
```
We set up two stacks one with accumulator values (two odd one even) and one with the numbers `4 4 2`. Each iteration we roll both stacks and add the top of the left stack to the top of the right stack.
```
(({}(({}({}))[({}[{}])]))[({}[{}])]<>(({}(({}({}))[({}[{}])]))[({}[{}])])<>)
```
This will increment each odd number by 4 and the one even number by 2. As we loop through we get a pattern of 2 odd 1 even, with every positive integer being hit. Thus we just loop `n` times with `n` being the input. This has an asymptotic probability of **2/3**.
[Answer]
# [Re:direction](https://esolangs.org/wiki/Re:direction), ~~20~~ 19 bytes
```
+++>>
 v>
 +>>
v>
<
```
[Try it online!](https://tio.run/##K0rVTcksSk0uyczP@/9fW1vbzo5LoQyIQQwgbfP/vxkA "Re:direction – Try It Online")
-1 by rearranging to remove a space.
Here's what happens, based on the input number:
* **3n**: It runs along the first line n times, picking up 2n `>`s, before going down from the first `+`. It then adds a `v`, and hits the bottom `<`, which points to itself to halt, outputting **2n**.
* **3n+1**: It again runs along the first line n times, picking up 2n `>`s, and then it passes the first `+` and goes down on the second one. It then adds a `v`. The loop on the third line doubles the number of `>`s in the queue to 4n before ending by consuming the `v`. On the fourth line, another `>` is added, for 4n+1, before ending in the same way as the previous case, outputting **4n+1**.
* **3n+2**: Mostly the same as the above case, except going down from the third `+` and then adding an extra `>` before joining the same path, making 2n+1, which becomes 4n+2, then 4n+3, and outputting **4n+3**.
[![Diagram](https://i.stack.imgur.com/4fzun.png)](https://i.stack.imgur.com/4fzun.png)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
ÆE;0ṭ2/FÆẸ
```
The probability of odds is **2/3**.
[Try it online!](https://tio.run/##AScA2P9qZWxsef//w4ZFOzDhua0yL0bDhuG6uP8xMDBSwrXFvMOH4oKsR/8 "Jelly – Try It Online")
### How it works
```
ÆE;0ṭ2/FÆẸ  Main link. Argument: n
ÆE          Compute the exponents of n's prime factorization.
  ;0        Append a 0.
     2/     Reduce all pairs by...
    ṭ         tack, appending the left argument to the right one.
            This inverts all non-overlapping pairs of exponents.
       F    Flatten the result.
        ÆẸ  Consider the result a prime factorization and compute the corresponding
            integer.
```
[Answer]
# JavaScript, 23 bytes
```
n=>n/2+n/2%2+(n%4&&n-1)
```
Output: 1, 3, 5, 2, 7, 9, 11, 4, 13, 15, 17, 6, 19, 21, 23, 8...
* For all n = 4k:
	+ f(n) = n/2 = 2k
* For all n = 4k + b
	+ f(n) = n/2 + b/2 + n - 1 = 3/2 \* (4k + b) + 1/2 \* b - 1 = 6k + 2b - 1
---
Challenge 2:
```
n=>n^(n>1)
```
Output: 1, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14
[Answer]
# C, 80 bytes
```
#define R if(k++==n)return
i,j,k;f(n){for(i=k=1,j=2;;i+=4,j+=2){R i;R i+2;R j;}}
```
Implementation of the example permutation from the question.
[Try it online!](https://tio.run/##HUxBCsMwDLvnFaZjYOMM1rKdPH9iPxhtM5Jm6QjdqfTtWVqBhJBs9Zd335dyGkbn0whP8A4nZtVEeVx@ORlvg53EYaLVzRm9TtraoJ2IZ73ZwNrRWv@kkruqQbat@LTA5@UTklkNVOxBlMPWGcCorUB83K8CzJGOYsc310uHzXmAxoLDSCRmK38)
[Answer]
## Batch, 36 bytes
```
@cmd/cset/an=%1*2,(-~n*!!(n%%3)+n)/3
```
Implements the sequence given in the question.
[Answer]
## CJam (21 bytes)
```
{2b_(&!\_,2*\1$~+2b?}
```
[Online demo](http://cjam.aditsu.net/#code=32%2C%3A)%0A%0A%7B2b_(%26!%5C_%2C2*%5C1%24~%2B2b%3F%7D%0A%0A%25p) showing the first 32 outputs. This is an anonymous block (function).
This is also a solution to challenge 1: the numbers mapped to even numbers are the powers of 2, so the density of even numbers in the first n outputs is lg(n)/n which tends to zero.
### Dissection
```
{         e# Declare a block; let's call the input x
  2b      e#   Convert to base 2
  _(&     e#   Copy, pull out first digit (1) and set intersection with rest
  !       e#   Boolean not, so empty set (i.e. power of 2) maps to 1 and non-empty
          e#   to 0
  \_,2*   e#   Take another copy, find its length, and double it
  \1$~+   e#   Take the original base 2 array and append ~(2L) = -2L-1
  2b      e#   Convert from base 2, to get 2x-2L-1
  ?       e#   Take the 2L if it was a power of 2, and the 2x-2L-1 otherwise
}
```
[Answer]
# Perl 40 bytes
```
$,=$";$i=4;{say$i-3,$i/2,($i+=4)-5;redo}
```
[Answer]
# [Brain-Flueue](https://github.com/DJMcMayhem/Brain-Flak), 88 bytes
```
({}<(((<>)[()])[()()])>)<>(((()())()()))<>{({})({})({})({}[()]<({}<>({})<>)>)}{}{}({}){}
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6O61kZDQ8PGTjNaQzMWRIAoO00bO6AoiKMJJoD8aqBSTSQMUm8D0m4HEgAaYKdZWw2EIF517f//Zv91c9JySlNLUwE "Brain-Flak – Try It Online")
## Explanation
This implements the same function as [my last answer](https://codegolf.stackexchange.com/a/141407/56656) but uses Brain-Flueue's FIFO model to cut some corners. Here are the first couple terms it generates.
```
2 3 1 4 7 5 6 11 9 8 15 13 10 17 15 ...
```
The first part of the code is just a bit of setup, we put `0,-1,-3` on the first stack and `2,4,4` on the second stack. The `2,4,4` will be used to cycle through even and odd numbers just as I did in my Brain-Flak answer.
We then loop n times, each time adding the top of the left stack to the right stack. Since Brain-Flueue uses queues as opposed to stacks the values naturally roll as we touch them preventing the need for extra code.
[Answer]
# [Lean](https://leanprover.github.io/), 85 bytes
```
import data.nat.prime
λx,list.prod$list.map(λp,if p<4then 5-p else p)$nat.factors x
```
[Try it online!](https://tio.run/##NYxLCoNAEAX3OUVDXCi8SNpfPiQnCVkMsYcMjNroELybd/BKEwxkVxRUeTF9jK7TYQzUmmDy3oRcR9fJrhVL9npflxneTZsd2uRHndF0XRTOkt6q8Jae6oOS@ElIs2RbWPMKwzjRHPfyMZ7Sf0iWHowCJSrUaHDCGRfwEczgAlyCK3ANbp5Z/AI "Lean – Try It Online")
Swaps 2s with 3s in the prime factorization.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
ǐ:4<꘍Π
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLHkDo0POqYjc6gIiwiIiwiMTQiXQ==) or [run it from 1 to 100](https://vyxal.pythonanywhere.com/#WyIiLCLigoHJvjrGmyIsIseQOjQ86piNzqAiLCI7WsabYCA9PiBgajvigYsiLCIiXQ==).
Port of Dennis's Jelly answer. ~~`1J` is needed due to a bug in Vyxal where the product of `[]` returns `0` instead of `1`.~~ Bug fixed.
## How?
```
ǐ:4<꘍Π
ǐ      # Get the prime factors with duplicates
 :     # Duplicate
  4<   # For each item, is it less than 4? (Vectorizes, produces a list of 1s and 0s)
    ꘍  # Bitwise XOR the two arrays (vectorizes)
     Π # Product of resulting array
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
Þ∞y$:ẇYf
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyI1IiwiIiwiw57iiJ55JDrhuodZZiIsIiIsIiJd)
Challenge number 1 - a list with an odd probability 1. Creates an infinite list.
The sequence generated looks like \$2, \color{red}{1}, \color{black}{4}, \color{red}{3, 5, 7}, \color{black}6, \color{red}{9, 11, 13, 15, 17}, \color{black}{8}\cdots\$, interleaving the even numbers with the odd numbers in ever-growing chunks.
If we take the first \$k^2+k\$ terms for integer \$k\$, \$\sum\_{n=0}^{k}2n+1 = k^2\$ of them will be odd and \$k\$ will be even, so the proportion of odd numbers is \$\frac{k}{k+1}\$. As \$k \to ∞ \$, \$ \frac{k}{k+1} \to 1\$.
```
Þ∞       # Infinite list of positive integers
  y$     # Uninterleave, pushing odds and evens, and swap the odds on top
    :ẇ   # Cut odds into sequences of lengths [1, 3, 5, 7...]
      Yf # Interleave and flatten.
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~46~~ ~~104~~ 55 bytes
```
lambda n:2*((n-int(n**.5))+.5,n**.5-1)[n!=1>0==n**.5%1]
```
[Try it online!](https://tio.run/##ZY7BasMwEETv/YptQCDZiioZcikooC/oocc0BzVeuwZ5rchqoV/vKg5JCz0sDLuzbyZ@54@JmqWzb0vw43vrgZ6binPaDpQ5VZXaCVGrnVzl1ogDPVqz19auC2aOiwMLo4@8k5A89ciNBKO1ro0QDzEVDmxe2hYiphNS9j2WB6aajjG1AQa8mFUVkHg3hIyJ34sQa6QT4ulyc7@w19GHgHOGOM1DHr4Qyhp7TEBTLhpmPH8indac9prxD33zOnkrfU25tK4P@ijK3AP/8OaV55Yf "Python 2 – Try It Online")
Misread the question, now properly implemented a function that can be used to generate a sequence instead of one that generates a sequence. Also excluded `0` from the set of possible outputs. 
Probability of finding an odd positive integer now converges to `1`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
Ḥ€’ĖUẎQ⁸ị
```
[Try it online!](https://tio.run/##ASEA3v9qZWxsef//4bik4oKs4oCZxJZV4bqOUeKBuOG7i////zE "Jelly – Try It Online")
Implements `1, 3, 2, 5, 7, 4, 9, 11, 6, ...` (probability 2/3).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 11 bytes
```
·ÅÉā‚ø˜Ù¹<è
```
[Try it online!](https://tio.run/##ASEA3v8wNWFiMWX//8K3w4XDicSB4oCaw7jLnMOZwrk8w6j//zI "05AB1E – Try It Online")
[Answer]
# [Pyth](https://pyth.readthedocs.io), 9 bytes
```
*Fmxd<d4P
```
**[Try it here!](https://pyth.herokuapp.com/?code=%2aFmxd%3Cd4P&test_suite=1&test_suite_input=4&debug=0)** or **[Test more in one go!](https://pyth.herokuapp.com/?code=%2aFmxd%3Cd4P&input=4&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11%0A12%0A13%0A14%0A15%0A16%0A17%0A18%0A19%0A20%0A21%0A22%0A23%0A24%0A25%0A26%0A27%0A28%0A29%0A30&debug=0)**
You can use this code to verify the ratio of odd numbers up to a certain point. Substitute `10000` with your desired limit (don't set it much higher, because it memory errors).
```
Km*Fmxk<k4PdS10000clf%T2KlK
```
[Try it here](http://pyth.herokuapp.com/?code=Km%2aFmxk%3Ck4PdS10000clf%25T2KlK&input=4&test_suite=1&debug=0).
The above gives roughly **0.667**. The true probability of odd occurrences is **2/3**. This approach is an equivalent implementation of [Dennis' answer](https://codegolf.stackexchange.com/a/141387/59487).
---
# Explanation
```
*Fmxd<d4P   Full program.
        P   Prime factors.
  m         Map over ^.
   x        Bitwise XOR between:
    d          The current prime factor.
     <d4       The integer corresponding to the boolean value of current factor < 4.
*F          Product of the list.
```
[Answer]
# Java 8, 20 bytes
```
n->n%4>0?n+n/2|1:n/2
```
Port of [*@nwellnhof*'s C answer](https://codegolf.stackexchange.com/a/141406/52210).  
Some things I tried myself ended up being a few bytes longer or slightly incorrect..
Implements: `1,3,5,2,7,9,11,4,13,15,17,6,19,21,23,8,25,27,29,10,31,33,35,12,37,...`  
with a probability of `3/4`.
[Try it here.](https://tio.run/##LU7LDoIwELz7FRsSE0gFhehFBL9ALhyNh1rAFGFLaCExyrfj8kj2kZ2dyUzJe@6qJscye4@i4lrDjUv8bgAkmrwtuMghmc4ZAGFPE52QkIGaShtupIAEECIY0Y1xe4wPV2S4D37@meYYLsyme1bEXAW9khnUZGanppX4uj@AO4tTodrZR0Z@CPISBSdajK1fgPSjTV57qjNeQ1Jjo0fBHGbtrDnZkm3YDOMf)
[Answer]
## Lua, ~~67~~ 53 bytes
~~Explanation coming when I'm done golfing this :)~~
This program takes an integer via command-line arguments as input and prints out the nth element of the exemple sequence to STDOUT
```
n=...print(n%3<1 and n/3*2or n+math.floor(n/3)+n%3-1)
```
### Explanations
```
n=...                              -- shorthand for the argument
print(                             -- prints out the result of the following ternary
     n%3<1                         -- if n is divisible by 3
       and n/3*2                   -- prints out the corresponding even number
       or n+math.floor(n/3)+n%3-1) -- else prints out the odd number
```
The even numbers of this sequence are both the `n`th even number and the `n` multiple of 3, hence the formula `n%3*2` is sufficient to generate them.
For odd numbers, it's a little bit more tough. Based on the fact that we can find them depending on the current `n`, we have the following table :
```
n       |  1   2   4   5   7   8   10   11  
target  |  1   3   5   7   9   11  13   15
target-n|  +0  +1  +1  +2  +2  +3  +3   +4
```
Let's call the value `target-n` `i`, we can see that each time `n%3==2`, `i` is incremented. There goes our formula :
```
n+math.floor(n/3)+n%3-1
```
The odd numbers are based on `n` on which we add `i`.
The value of `i` increments at the same rate as the euclidian division by 3, with an offset. `math.floor(n/3)` gives us the rate of increment and `n%3-1` gives us the offset, making it happens on `n%3==2` instead of `n%3==0`.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 6 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ÒD4‹^P
```
Port of [*@Dennis*' Jelly answer](https://codegolf.stackexchange.com/a/141387/52210), so make sure to upvote him!
The probability of odds is therefore also \$\frac{2}{3}\$.
[Try it online (outputs an infinite list after timing out)](https://tio.run/##yy9OTMpM/f@oY965rf8PT3IxedSwMy7g/38A) or [only output the first \$n\$ values instead](https://tio.run/##yy9OTMpM/e9zbuv/w5NcTB417IwL@P/fyBQA).
**Explanation:**
```
Ò       # Get the prime factors of the (implicit) input-integer, counting multiplicities
 D      # Duplicate this list
  4‹    # Check for each value whether it's smaller than 4 (1 for 2 and 3; 0 otherwise)
    ^   # Bitwise-XOR the two lists at the same indices
        # (2 becomes 3; 3 becomes 2; everything else remains unchanged)
     P  # And take the product of this
        # (after which the result is output implicitly)
```
[Answer]
# x86 32-bit machine code, 12 bytes
```
a8 03 74 05 8d 04 40 0c 02 d1 e8 c3
```
A port of [nwellnhof's C answer](https://codegolf.stackexchange.com/a/141406/104752). Uses the `regparm(1)` calling convention – argument in EAX, result in EAX.
**[Try it online!](https://tio.run/##jZTvbpswEMC/8xQ3pko4oVUB9UtS9iJZhIyxiScHI@NMtFVffeyACOKStkGyzz5@918yuy8Z67qfsmLqVHB4bmwh9cPhl@eolMx7nUebIwSe/7t6sLy1/nCSleUqa14qS1uodG24kO34q1Q6pwqEe4vca@xek@E6bGIzCMDP8sYCVSEkk@rPK9Qagz9OGsUpcNqGsMMd1v0ZVhDvJ0CbwccYcTSeQzQHM1pHk8pwe5ENn9m8EcAZsmiwdMDUpGsx5ABGF5haYG6gaA5U65pV9los9THZvMFQxTWucLiCMxc55aMr5OZWal2P7Z0NW3YoXXIENp@UEW/cNrjp4vjOo19aJhu3qo/VG7bsdJPn5/IvyjB62eur9U7xydbzsoxaa2R@sjzLgsDwsqbmGESEEMCCQQT93iL6LclvR6Pb0fh2NJnQXgZf8StR7Z72pDcgkMKbCDF/XBGuGFfyjr3pvRyprAa31JQMe3ugBlYrvPwl3pvXt1PgvAfCoqPHLYofKTyhXK@JC0gEoi2K5xSSGA9IQG3wlwj8O3nH/BCCPjPbZ0ZC8AGntJNpmsR7rOo8vJOpMI737nXdPyYULZvu/pjEuOGTlQ5P1H8 "C (gcc) – Try It Online") (contains all five functions)**
Assembly:
```
f:
    test al, 3                  # Take the low two bits of the value.
    jz point0                   # Jump if they are both 0.
    lea eax, [eax + eax * 2]    # Triple the value in EAX.
    or al, 2                    # Set the twos bit in EAX.
point0:
    shr eax, 1                  # Shift EAX right by 1 bit.
    ret                         # Return.
```
## The opposite
I also found an 11-byte function with a 1/4 odd probability:
```
0f bc c8 d3 e8 83 f1 01 d3 e0 c3
```
Assembly:
```
fe:
    bsf ecx, eax    # Set ECX to the position of the lowest 1 bit in EAX.
    shr eax, cl     # Shift EAX right by that quantity, removing all ending 0 bits.
    xor ecx, 1      # Invert the lowest bit of ECX.
    shl eax, cl     # Shift EAX left, introducing a modified number of ending 0 bits.
    ret             # Return.
```
# 1st extra challenge: 18 bytes
```
f3 0f b8 c8 d1 e0 0f bd d0 d1 e2 48 29 d0 e2 01 92 c3
```
Implements a function several other answers have used, mapping powers of 2 to even numbers in ascending order and other numbers to odd numbers in ascending order: \$ 
f\_1(2^m) = 2m+2 \$, and for \$ n \in [2^m, 2^{m+1})\$, \$ f\_1(n) = 2n - 2m - 3\$.
Assembly:
```
f1:
    popcnt ecx, eax # Set ECX to the number of 1 bits in EAX.
    shl eax, 1      # Shift EAX left by 1, doubling it to 2n.
    bsr edx, eax    # Set EDX to the position of the highest 1 bit in EAX -- m+1.
    shl edx, 1      # Shift EDX left by 1, doubling it to 2m+2.
    dec eax         # Decrement EAX to 2n-1.
    sub eax, edx    # Subtract EDX from EAX, making it 2m-2n-3.
    loop point1     # Decrement ECX and jump if it's nonzero -- if the value was not a power of 2.
    xchg eax, edx   # Exchange EAX and EDX, making EAX 2m+2.
point1:
    ret             # Return.
```
# 2nd extra challenge: 6 bytes
```
83 f0 01 74 fb c3
```
Produces \$ 1, 3, 2, 5, 4, 7, 6, ... \$
Assembly:
```
f2:
    xor eax, 1  # Invert the lowest bit of the value.
    jz f2       # Jump back if that makes it 0 (and it will be flipped back to 1).
    ret         # Return.
```
# 3rd extra challenge: 12 bytes
```
0f bd c8 d3 d8 19 d2 d3 c0 29 d0 c3
```
Rearranges the bits of the value by moving the second-highest bit to the bottom. This produces 1 odd, 1 even, 1 odd, 2 even, 2 odd, 4 even, 4 odd, ...
Assembly:
```
f3:
    bsr ecx, eax    # Set ECX to the position of the highest 1 bit in EAX.
    rcr eax, cl     # Rotate EAX and the carry flag CF together left by that quantity.
# Note that the value of CF after BSR is officially undefined,
#  but it is consistently 0 on at least some systems, which is what this function needs.
# This puts the second-highest bit of the value into CF,
#  while the highest bit goes to the bottom position and the lower bits go to the top.
    sbb edx, edx    # Subtract EDX+CF from EDX, making its value -CF.
    rol eax, cl     # Rotate EAX right by the same value, not including CF in the rotation.
# EAX now contains all the bits of the original value except the second-highest,
#  followed by a single 0 bit; if the original value was 1, EAX is now 1.
    sub eax, edx    # Subtract EDX from EAX, replacing the removed bit at the bottom.
    ret             # Return.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 39 bytes
```
lambda n:[n/3*4+1,n/3*4+3,n/3*2+2][n%3]
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHPKjpP31jLRNtQB0Ibg2kjbaPY6DxV49j/aflFChUKmXkKRYl56akaRgaaVlwKBUWZeSUaaRoVmpr/AQ "Python 2 – Try It Online")
outputs :
```
1, 3, 2, 5, 7, 4, 9, 11, 6, 13, 15, 8, 17, 19, 10, 21, 23, 12, 25, 27, 14, 29, 31, 16 ...
```
Odd probability = 2/3
If generating the sequence without taking an inputs is accepted, I also have:
### [Python 2](https://docs.python.org/2/), 34 bytes
```
i=3
while 1:print-~i/2,i-2,i,;i+=4
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P9PWmKs8IzMnVcHQqqAoM69Ety5T30gnUxeIdawztW1N/v8HAA "Python 2 – Try It Online")
Outputs:
```
2 1 3 4 5 7 6 9 11 8 13 15 10 17 19 12 21 23 14 25 27 16 29 31 18 33 35 20 37 39 22 41 ...
```
Odd probability = 2/3
[Answer]
# Mathematical function, 48 bytes
You said "function", so I'm making a function. Takes an input `x` and returns the `x+1`th term. It does mess up when given a non-integer.
```
       x>=1>(x-1) mod 2:x+2
f(x)={ x>=1>x mod 2:x       }
       x=0:1
```
Excluding non-meaningful whitespace.
Here's a graph:
[![enter image description here](https://i.stack.imgur.com/vt4Qb.png)](https://i.stack.imgur.com/vt4Qb.png)
There are always one or two more odd numbers than even ones.
]