一生几回质数年

(1)

吃午饭的时候,同事 ZY 神秘秘地告诉我:

你知道吗?R 语言可以算出一个函数的导函数!

什么?!我结结实实吃了一惊。作为十几年的 R 语言用户,虽然偶尔会撞见零星几个藏在犄角旮旯里的功能,但是距离上回这么惊讶已经有年头了。

怎么做到的?

ZY 发给我一行代码:

mosaicCalc::D(sin(x) ~ x)
## function (x) 
## cos(x)

厉害厉害!那么有没有它的反函数呢?就是已知导函数,求原函数?我问。

ZY 被噎住了。然而没多久,就发给我另一行代码:

mosaicCalc::antiD(a * x ^ 2 ~ x, a = 3)
## function (x, a = 3, C = 0) 
## a * 1/3 * x^3 + C

果然有啊!

后来我发现,其实不用 mosaicCalc,基础函数包就有,只是写法不一样:

D(expression(sin(x)), "x")
## cos(x)

不管怎么样,看着这两种自从考完大学高数课就再也没有用过的运算,顿时感到午后的阳光洒满了校园。

接着我提出了一个新问题:

R 语言会做分解质因数吗?

(2)

之所以这么问,是因为前两天辅导老大功课的时候,我遇见了一道题,让我耿耿于怀总放不下。

题目是这样的:\(m\)\(k\) 都是整数,且 \(m^m(m^k-k)=2012\),求 \(m\)\(k\)

这一看就是 2012 年的题目嘛,也不更新一下。

老大挑衅:你管它是哪年的,你会做吗?

老父亲试一下:首先想到的就是分解质因数,2012 = 2 x 2 x … 503 还能分解吗?

老大看着我手忙脚乱地在纸上从 7 开始试,便不声不响地一番操作,然后把一件物事递到我鼻子底下。

我停下笔一看,是他的计算器:

什么?现在的计算器已经逆天到可以分解质因数了?

在儿子的鄙视下,万般念头涌入脑海:当年学过的那些技能啊,珠算、手动开平方、查对数表……全都白学了。

现在这个清单上又多了个分解质因数。

于是这事儿就这么了了:\(m\) 肯定是 2 咯,而 \(2^k\) 稍微超过 503 一丢丢的,\(k\) 只能是 9 咯。

然而草还是种下了:分解质因数,R 语言能整么?

(3)

当然能整。要是不能整,咱就自己写个函数整一整。但是最好找个现成的,白用。

一搜,果然有!

library(gmp)
factorize(2012)
## Big Integer ('bigz') object of length 3:
## [1] 2   2   503

爽极了。我心念一动;考题一般都拿当年的年份做文章,那么押个题试试? 2021……

factorize(2021)
## Big Integer ('bigz') object of length 2:
## [1] 43 47

不由得一愣:2021 = 43 x 47?

2021 居然是个半质数!只能分解成两个质因数 43 和 47!而且这一对宝贝还是一对相邻的质数!而且 20 和 21 本身还是相邻的整数!

惊天大发现啊!今年的年终盘点,终于可以感觉没有虚度了!

那么,这辈子能遇见多少个半质数年以及质数年呢?撸起袖子,把 1980 到 2079 这 100 年都做个分解:

year <- 1980:2079
year_p <- sapply(year, factorize)
n_p <- sapply(year_p, length)
year_print <- paste(year, sapply(year_p, paste, collapse = ' x '), sep = ' = ')

质数年有 13 个,他们是:

year[n_p == 1]
##  [1] 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069

质数年有 27 个,他们是:

year[n_p == 2]
##  [1] 1981 1982 1983 1985 1991 1994 2005 2018 2019 2021 2026 2031 2033 2038 2041
## [16] 2042 2045 2047 2049 2051 2059 2062 2066 2071 2073 2077 2078

这 100 年的质因数表附在文末,供大家把玩。那么看看吧,你这一生会经历多少质数年,多少半质数年?


附录

year_print
##   [1] "1980 = 2 x 2 x 3 x 3 x 5 x 11"                   
##   [2] "1981 = 7 x 283"                                  
##   [3] "1982 = 2 x 991"                                  
##   [4] "1983 = 3 x 661"                                  
##   [5] "1984 = 2 x 2 x 2 x 2 x 2 x 2 x 31"               
##   [6] "1985 = 5 x 397"                                  
##   [7] "1986 = 2 x 3 x 331"                              
##   [8] "1987 = 1987"                                     
##   [9] "1988 = 2 x 2 x 7 x 71"                           
##  [10] "1989 = 3 x 3 x 13 x 17"                          
##  [11] "1990 = 2 x 5 x 199"                              
##  [12] "1991 = 11 x 181"                                 
##  [13] "1992 = 2 x 2 x 2 x 3 x 83"                       
##  [14] "1993 = 1993"                                     
##  [15] "1994 = 2 x 997"                                  
##  [16] "1995 = 3 x 5 x 7 x 19"                           
##  [17] "1996 = 2 x 2 x 499"                              
##  [18] "1997 = 1997"                                     
##  [19] "1998 = 2 x 3 x 3 x 3 x 37"                       
##  [20] "1999 = 1999"                                     
##  [21] "2000 = 2 x 2 x 2 x 2 x 5 x 5 x 5"                
##  [22] "2001 = 3 x 23 x 29"                              
##  [23] "2002 = 2 x 7 x 11 x 13"                          
##  [24] "2003 = 2003"                                     
##  [25] "2004 = 2 x 2 x 3 x 167"                          
##  [26] "2005 = 5 x 401"                                  
##  [27] "2006 = 2 x 17 x 59"                              
##  [28] "2007 = 3 x 3 x 223"                              
##  [29] "2008 = 2 x 2 x 2 x 251"                          
##  [30] "2009 = 7 x 7 x 41"                               
##  [31] "2010 = 2 x 3 x 5 x 67"                           
##  [32] "2011 = 2011"                                     
##  [33] "2012 = 2 x 2 x 503"                              
##  [34] "2013 = 3 x 11 x 61"                              
##  [35] "2014 = 2 x 19 x 53"                              
##  [36] "2015 = 5 x 13 x 31"                              
##  [37] "2016 = 2 x 2 x 2 x 2 x 2 x 3 x 3 x 7"            
##  [38] "2017 = 2017"                                     
##  [39] "2018 = 2 x 1009"                                 
##  [40] "2019 = 3 x 673"                                  
##  [41] "2020 = 2 x 2 x 5 x 101"                          
##  [42] "2021 = 43 x 47"                                  
##  [43] "2022 = 2 x 3 x 337"                              
##  [44] "2023 = 7 x 17 x 17"                              
##  [45] "2024 = 2 x 2 x 2 x 11 x 23"                      
##  [46] "2025 = 3 x 3 x 3 x 3 x 5 x 5"                    
##  [47] "2026 = 2 x 1013"                                 
##  [48] "2027 = 2027"                                     
##  [49] "2028 = 2 x 2 x 3 x 13 x 13"                      
##  [50] "2029 = 2029"                                     
##  [51] "2030 = 2 x 5 x 7 x 29"                           
##  [52] "2031 = 3 x 677"                                  
##  [53] "2032 = 2 x 2 x 2 x 2 x 127"                      
##  [54] "2033 = 19 x 107"                                 
##  [55] "2034 = 2 x 3 x 3 x 113"                          
##  [56] "2035 = 5 x 11 x 37"                              
##  [57] "2036 = 2 x 2 x 509"                              
##  [58] "2037 = 3 x 7 x 97"                               
##  [59] "2038 = 2 x 1019"                                 
##  [60] "2039 = 2039"                                     
##  [61] "2040 = 2 x 2 x 2 x 3 x 5 x 17"                   
##  [62] "2041 = 13 x 157"                                 
##  [63] "2042 = 2 x 1021"                                 
##  [64] "2043 = 3 x 3 x 227"                              
##  [65] "2044 = 2 x 2 x 7 x 73"                           
##  [66] "2045 = 5 x 409"                                  
##  [67] "2046 = 2 x 3 x 11 x 31"                          
##  [68] "2047 = 23 x 89"                                  
##  [69] "2048 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2"
##  [70] "2049 = 3 x 683"                                  
##  [71] "2050 = 2 x 5 x 5 x 41"                           
##  [72] "2051 = 7 x 293"                                  
##  [73] "2052 = 2 x 2 x 3 x 3 x 3 x 19"                   
##  [74] "2053 = 2053"                                     
##  [75] "2054 = 2 x 13 x 79"                              
##  [76] "2055 = 3 x 5 x 137"                              
##  [77] "2056 = 2 x 2 x 2 x 257"                          
##  [78] "2057 = 11 x 11 x 17"                             
##  [79] "2058 = 2 x 3 x 7 x 7 x 7"                        
##  [80] "2059 = 29 x 71"                                  
##  [81] "2060 = 2 x 2 x 5 x 103"                          
##  [82] "2061 = 3 x 3 x 229"                              
##  [83] "2062 = 2 x 1031"                                 
##  [84] "2063 = 2063"                                     
##  [85] "2064 = 2 x 2 x 2 x 2 x 3 x 43"                   
##  [86] "2065 = 5 x 7 x 59"                               
##  [87] "2066 = 2 x 1033"                                 
##  [88] "2067 = 3 x 13 x 53"                              
##  [89] "2068 = 2 x 2 x 11 x 47"                          
##  [90] "2069 = 2069"                                     
##  [91] "2070 = 2 x 3 x 3 x 5 x 23"                       
##  [92] "2071 = 19 x 109"                                 
##  [93] "2072 = 2 x 2 x 2 x 7 x 37"                       
##  [94] "2073 = 3 x 691"                                  
##  [95] "2074 = 2 x 17 x 61"                              
##  [96] "2075 = 5 x 5 x 83"                               
##  [97] "2076 = 2 x 2 x 3 x 173"                          
##  [98] "2077 = 31 x 67"                                  
##  [99] "2078 = 2 x 1039"                                 
## [100] "2079 = 3 x 3 x 3 x 7 x 11"
comments powered by Disqus