前两个有两个自由分母的连续数字是:
14 = 2 × 7
15 = 3 × 5
前三个连续数字有三个自由分母:
644 = 2 × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19
如何找到前四个连续数字,正好有四个自由分母?
答案 0 :(得分:1)
不是644 = 2 x 7 x 23的情况。而是644 = 2 x 2 x 7 x 23 - 你正在寻找具有不同的素数除数的数字可能会重复< / em>的。事实上,1309,1310,1311是前3个数字,它们具有3个不同的非重复因子,并且不可能为4执行此操作,因为任何4个连续数字的运行将具有4的重复因子。
为了解决找到4个连续数字的问题,每个数字都有4个不同的(虽然可能是重复的)素数因子,你可以使用Sieve of Eratosthenes的修改版本,它可以跟踪不同除数的数量:
Function SieveK(n As Long, k As Long) As Long
'Implements a modified Sieve of Erasthones
'to return the first of k successive numbers
'less than or equal to n, each of which has
'k distinct prime factors.
'Returns -1 if no such number exists
Dim nums As Variant
Dim i As Long, p As Long
Dim run As Long
Dim limit As Long
Dim primes As Variant
primes = Array(2, 3, 5, 7, 11, 13) 'small primes
p = 1
For i = 0 To k - 2
p = p * primes(i)
Next i
limit = Int(1 + n / p)
ReDim nums(2 To n) As Long
p = 2 'first prime
Do While p < limit
'mark subsequent multiples of p by adding 1
For i = 2 * p To n Step p
nums(i) = nums(i) + 1
Next i
'find next p -- which will be next 0
p = p + 1
Do While nums(p) <> 0
p = p + 1
Loop
Loop
'At this stage, all numbers are marked.
'primes are marked by 0 and composites are marked
'by the number of distinct prime factors.
'Check for a run of k ks
run = 0
For i = 2 To n
If nums(i) = k Then
run = run + 1
If run = k Then 'we have a winner!
SieveK = i - k + 1
Exit Function
End If
Else
'reset run counter
run = 0
End If
Next i
SieveK = -1
End Function
SieveK(10^6,4)
在不到一秒的时间内评估为134043。该函数不会产生这个或接下来的3个数的因子分解,但这些很容易找到。 SieveK(10^8,5)
的计算结果为-1,因此不会有5个连续数字中的1亿个运行,每个数字都有5个不同的素因子。
备注:在这个答案的早期版本中,我有一个逻辑缺陷(它没有阻止输出正确)。也就是说 - 我只过了n的平方根,但是,例如,数m看起来像px的2x3x5xp然后m可能有4个不同的素因子,即使p可能超过n的平方根。我修改了算法来处理这种可能性。
答案 1 :(得分:0)
你走了:
134043 = 3 x 7 x 13 x 491
134044 = 2 x 23 x 31 x 47
134045 = 5 x 17 x 19 x 83
134046 = 2 x 3 x 11 x 677