Forgot password?
 Create new account
View 2418|Reply 6

[函数] 具有函数迭代的不变性的函数

[Copy link]

462

Threads

969

Posts

9934

Credits

Credits
9934

Show all posts

青青子衿 Posted at 2013-10-13 12:29:40 |Read mode
Last edited by hbghlyj at 2025-4-7 05:47:48如何构造出满足 $f(f(f(f(f(x)))))=f(x)$ 的函数 $f(x)$例如:$f(x)=\frac{1+x}{1-x}$那么,像 $f(f(x))=f(x), f(f(f(x)))=f(x), f(f(f(f(x))))=f(x)$

65

Threads

414

Posts

3556

Credits

Credits
3556

Show all posts

Tesla35 Posted at 2013-10-13 12:35:31
回复 1# 青青子衿


    那岂不是很多。。
反函数,倒数,负倒数,相反数。
分一下迭代奇偶吧

87

Threads

2383

Posts

110K

Credits

Credits
13325

Show all posts

其妙 Posted at 2013-10-13 14:07:15
看迭代周期噻,
妙不可言,不明其妙,不着一字,各释其妙!

3148

Threads

8497

Posts

610K

Credits

Credits
66188
QQ

Show all posts

hbghlyj Posted at 2025-4-7 05:52:31

$f∘f=f$

topologicalmusings.wordpress.com/2009/03/28/n … otent-endofunctions/
  1. import math
  2. def count_idempotent_functions(n):
  3.     """
  4.     Count the number of idempotent functions f: {1, ..., n} -> {1, ..., n}
  5.     satisfying f(f(x)) = f(x) for all x. The formula is:
  6.     sum_{k=1..n} [C(n, k) * k^(n - k)],
  7.     where C(n, k) is the binomial coefficient.
  8.     """
  9.     total = 0
  10.     for k in range(1, n + 1):
  11.         total += math.comb(n, k) * (k ** (n - k))
  12.     return total
  13. for n in range(1, 11):
  14.     print(f"n = {n}, number of idempotent functions = {count_idempotent_functions(n)}")
Copy the Code

3148

Threads

8497

Posts

610K

Credits

Credits
66188
QQ

Show all posts

hbghlyj Posted at 2025-4-7 05:56:57

$f∘f∘f=f$

  1. from math import comb, factorial
  2. def count_functions(n):
  3.     total = 0
  4.     # k is the number of fixed points, l is the number of 2-cycles
  5.     for k in range(n+1):
  6.         for l in range((n//2)+1):
  7.             s = k + 2*l
  8.             if s > n:
  9.                 break
  10.             # Choose which s elements form the cycles
  11.             ways_subset = comb(n, s)
  12.             # Within those s elements, choose k of them to be fixed points
  13.             # and partition the remaining 2l into l 2-cycles
  14.             ways_cycle_struct = comb(s, k) * factorial(2*l) // (2**l * factorial(l))
  15.             # The remaining n-s elements each map into any of those s cycle elements
  16.             ways_outside = s ** (n - s)
  17.             total += ways_subset * ways_cycle_struct * ways_outside
  18.     return total
  19. for n in range(1, 11):
  20.     print(f"n={n}, count={count_functions(n)}")
Copy the Code

3148

Threads

8497

Posts

610K

Credits

Credits
66188
QQ

Show all posts

hbghlyj Posted at 2025-4-7 06:15:24

$f∘f∘f∘f=f$

  1. import math
  2. def count_functions(n):
  3.     """
  4.     Counts the number of functions f: {1..n} -> {1..n} such that
  5.     f(f(f(f(x)))) = f(x) for all x in {1..n}.
  6.    
  7.     Key observations:
  8.     - The condition f^4(x) = f(x) for all x is satisfied exactly by
  9.       functions whose cycles are only of length 1 or 3.
  10.     - Moreover, any element not in a cycle must map directly (in one step)
  11.       to an element on one of these 1- or 3-cycles (no longer chains are allowed).
  12.    
  13.     Let c1 be the number of 1-cycles and c3 be the number of 3-cycles.
  14.     Then we choose c1 + 3*c3 distinct elements from n to form the cycles.
  15.     Among those c1 + 3*c3 chosen elements, we choose c1 to be 1-cycles;
  16.     the remaining 3*c3 form c3 disjoint 3-cycles.
  17.     The other n - (c1 + 3*c3) elements each map in one step to any
  18.     of the cycle elements.
  19.    
  20.     The count of ways to form these cycles is:
  21.    
  22.       Choose(c1 + 3*c3, c1) * ( (3*c3)! / (3^(c3) * c3!) ) * (c1 + 3*c3)^(n - (c1 + 3*c3))
  23.    
  24.     Summing over all valid c1, c3 (with c1 + 3*c3 <= n) gives the total.
  25.     """
  26.     total = 0
  27.     for c3 in range(n // 3 + 1):
  28.         for c1 in range(n - 3*c3 + 1):
  29.             cycle_count_choice = math.comb(c1 + 3*c3, c1)
  30.             
  31.             # Number of ways to arrange c3 disjoint 3-cycles among 3*c3 distinct elements:
  32.             # (3*c3)! / (3^(c3) * c3!)
  33.             if c3 > 0:
  34.                 cycle_3_ways = math.factorial(3*c3) // (3**c3 * math.factorial(c3))
  35.             else:
  36.                 cycle_3_ways = 1  # if c3=0, there's exactly 1 way (no 3-cycles)
  37.             
  38.             # For the remaining n - (c1 + 3*c3) elements, each one can map to any
  39.             # of the c1 + 3*c3 cycle elements
  40.             ways_for_remaining = (c1 + 3*c3)**(n - (c1 + 3*c3))
  41.             
  42.             total += cycle_count_choice * cycle_3_ways * ways_for_remaining
  43.     return total
  44. for n in range(1, 11):
  45.     print(f"n = {n}, count = {count_functions(n)}")
Copy the Code

3148

Threads

8497

Posts

610K

Credits

Credits
66188
QQ

Show all posts

hbghlyj Posted at 2025-4-7 06:32:07

$f∘f∘f∘f∘f=f$

Last edited by hbghlyj at 2025-4-7 07:16:40n元集上的函数$f$的个数
1,4,25,224,2601,36352,588337,10809360,222264145,5057068256
在OEIS中没有找到,是不是算错了
  1. import math
  2. def precompute_ways_chains(max_n):
  3.     """
  4.     Precompute the number of ways to attach 'rem = n - M' elements as chains (of length up to 4)
  5.     into any of M cycle nodes.
  6.     Each of the 'rem' elements can form a chain of length 1..4 ending in a cycle node. We count
  7.     how many ways they can form those chains collectively.
  8.    
  9.     We'll store these counts in ways_chains_table[n][M].
  10.     """
  11.     # Precompute factorials for speed
  12.     factorials = [1]*(max_n+1)
  13.     for i in range(1, max_n+1):
  14.         factorials[i] = factorials[i-1]*i
  15.     def multinomial(total, counts):
  16.         # multinomial coefficient = total! / (counts[0]! * counts[1]! * ... )
  17.         num = factorials[total]
  18.         for c in counts:
  19.             num //= factorials[c]
  20.         return num
  21.     ways_chains_table = [[0]*(max_n+1) for _ in range(max_n+1)]
  22.    
  23.     for n in range(1, max_n+1):
  24.         for M in range(n+1):
  25.             rem = n - M
  26.             if rem < 0:
  27.                 continue
  28.             total_ways = 0
  29.             # We partition rem into (s1, s2, s3, s4) where s1 + s2 + s3 + s4 = rem
  30.             # s1 = number of elements that map directly to a cycle node
  31.             # s2 = number of elements that map to one of those s1 elements
  32.             # s3 = number that map to an element among s2...
  33.             # s4 = number that map to an element among s3...
  34.             # Each chain ends at a cycle node, so effectively the chain length is from 1 up to 4.
  35.             for s1 in range(rem+1):
  36.                 for s2 in range(rem+1 - s1):
  37.                     for s3 in range(rem+1 - s1 - s2):
  38.                         s4 = rem - s1 - s2 - s3
  39.                         # The multinomial factor to choose which elements go to s1, s2, s3, s4
  40.                         multi = multinomial(rem, (s1, s2, s3, s4))
  41.                         # attachments factor = M^s1 * s1^s2 * s2^s3 * s3^s4
  42.                         
  43.                         # If s1=0 but s2>0, that implies references to 0 existing elements => invalid
  44.                         if s1 == 0 and s2 > 0:
  45.                             continue
  46.                         if s2 == 0 and s3 > 0:
  47.                             continue
  48.                         if s3 == 0 and s4 > 0:
  49.                             continue
  50.                         
  51.                         attach_factor = (M**s1) * (s1**s2) * (s2**s3) * (s3**s4)
  52.                         total_ways += multi * attach_factor
  53.             ways_chains_table[n][M] = total_ways
  54.    
  55.     return ways_chains_table
  56. def count_functions_power5(n, ways_chains_table):
  57.     """
  58.     Count the number of functions f: {1..n} -> {1..n} such that
  59.     f(f(f(f(f(x))))) = f(x) for all x.
  60.     Valid cycle lengths under this condition are 1, 2, and 4 because f^5(x)=f(x)
  61.     implies the cycle length divides (5-1)=4 or is 1 itself. Then every non-cycle
  62.     element must chain within up to 4 steps into a cycle node.
  63.     """
  64.     total = 0
  65.     # c1: count of 1-cycles, c2: count of 2-cycles, c4: count of 4-cycles
  66.     for c4 in range(n // 4 + 1):
  67.         for c2 in range((n - 4*c4) // 2 + 1):
  68.             c1_max = n - 2*c2 - 4*c4
  69.             for c1 in range(c1_max + 1):
  70.                 # M = total cycle elements
  71.                 M = c1 + 2*c2 + 4*c4
  72.                 # Choose M distinct elements out of n
  73.                 ways_choose_M = math.comb(n, M)
  74.                 # Among these M, choose c1 for 1-cycles
  75.                 ways_choose_c1 = math.comb(M, c1)
  76.                 # Remaining M-c1 = 2*c2 + 4*c4 for the 2- and 4-cycles
  77.                 # We first choose which 2*c2 among these (2*c2 + 4*c4) will form the 2-cycles
  78.                 ways_choose_2c2 = math.comb(2*c2 + 4*c4, 2*c2)
  79.                 # Count ways to form c2 disjoint 2-cycles from 2*c2 distinct elements:
  80.                 ways_2cycles = math.factorial(2*c2) // (2**c2 * math.factorial(c2))
  81.                 # Count ways to form c4 disjoint 4-cycles from 4*c4 distinct elements:
  82.                 ways_4cycles = math.factorial(4*c4) // (4**c4 * math.factorial(c4))
  83.                
  84.                 # Ways to attach the remaining n-M elements in chains of length <=4
  85.                 ways_attach = ways_chains_table[n][M]
  86.                
  87.                 total += (ways_choose_M *
  88.                           ways_choose_c1 *
  89.                           ways_choose_2c2 *
  90.                           ways_2cycles *
  91.                           ways_4cycles *
  92.                           ways_attach)
  93.     return total
  94. ways_chains_table = precompute_ways_chains(10)
  95. for i in range(1, 11):
  96.     print(f"n={i}, count={count_functions_power5(i, ways_chains_table)}")
Copy the Code

手机版Mobile version|Leisure Math Forum

2025-4-21 01:33 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list