Questions tagged «arithmetic»

与基本算术有关的挑战。

7
打假素!
简介/背景 在最近一次关于加密聊天的讨论中,我被挑战去讨论/帮助Fermat素数测试和Carmichael数字。该测试基于a^(p-1) mod p==1始终适用于素数p而不适用于复合材料的前提。现在,carmichael号码本质上是Fermat考验的最大敌人:您必须选择一个号码,a以使其不与素数p相抵a^(p-1) mod p!=1。现在,如果a不是互质,则基本上可以发现一个非平凡的因子p众所周知,保理可能相当困难。特别是在所有因素都足够大的情况下。您现在可能已经意识到,为什么在实践中不经常使用Fermat测试(还有更好的算法),这是因为作为防御者(在安全性方面)您需要为某些数字做与攻击者(即数量)。 因此,既然我们知道为什么这些数字有些引人入胜,我们将以最短的方式生成它们,因此只要需要,我们就可以记住生成的代码! Carmichael编号在OEIS上也称为A002997。已经 存在一个相关的挑战,但是这里的条目没有竞争力,因为它们是针对速度而非大小进行了优化的。同样的论点也适用于反方向,此处的输入可能会在速度上做出取舍,而有利于大小。 规格 输入项 这是一个标准 序列挑战,因此您将正整数或非负整数n作为输入。n可以根据需要选择0或1的索引(请指出)。 输出量 根据需要,您的输出将是n-th carmichael数或第一个ncarmichael数(请指出)。 规格 一个整数x是卡迈克尔数当且仅当x是复合材料和所有的整数y与gcd(x,y)=1,它认为y^(x-1) mod x==1。 谁赢? 这是 代码高尔夫球,因此以字节为单位的最短代码获胜!适用标准IO和漏洞规则。 测试用例 前几个carmichael号码是: 561,1105,1729,2465,2821,6601,8911,10585,15841, 29341,41041,46657,52633,62745,63973,75361,101101, 115921,126217,162401,172081,188461,252601,278545, 294409,314821,334153,340561,399001,410041,449065, 488881,512461
9 code-golf  math  sequence  number-theory  primes  code-golf  string  code-golf  math  combinatorics  permutations  restricted-complexity  code-golf  array-manipulation  code-golf  number  sequence  code-golf  number  sequence  code-golf  binary-matrix  code-golf  math  tips  javascript  algorithm  code-golf  string  code-golf  number  sequence  code-golf  math  arithmetic  parsing  code-golf  number  sequence  primes  code-golf  string  ascii-art  geometry  integer  code-golf  geometry  code-golf  number  array-manipulation  code-golf  math  geometry  code-golf  number  sequence  arithmetic  integer  code-golf  string  kolmogorov-complexity  code-golf  number  code-golf  number  chess  code-golf  sequence  decision-problem  subsequence  code-golf  math  number  primes  code-golf  primes  permutations  code-golf  integer  probability-theory  statistics  code-golf  string  code-golf  sequence  decision-problem  parsing  board-game  code-golf  binary  graph-theory  code-golf  board-game  classification  tic-tac-toe  code-golf  ascii-art  polyglot  code-golf  date  code-golf  geometry 

13
扩大比较链
与大多数语言不同,Python a<b<c会像在数学中一样进行评估,实际上是比较三个数字,而不是将布尔值a<b与进行比较c。用C(以及许多其他语言)编写此代码的正确方法是a<b && b<c。 在这个挑战中,您的任务是将这样任意长度的比较链从Python /直观表示扩展到如何用其他语言编写。 技术指标 您的程序必须要处理运算符:==, !=, <, >, <=, >=。 输入将具有仅使用整数的比较链。 不用担心一路比较的真实性,这纯粹是语法/语法上的挑战。 输入将没有任何空格,以防止答案因分割空格而使解析变得琐碎。 但是,您的输出可能只有一个空格,要么仅包含&&,要么包含比较运算符和&&,或者两者都不包含,但是要保持一致。 测试用例 Input Output --------------------------------------------------------------- 3<4<5 3<4 && 4<5 3<4<5<6<7<8<9 3<4 && 4<5 && 5<6 && 6<7 && 7<8 && 8<9 3<5==6<19 3<5 && 5==6 && 6<19 10>=5<7!=20 10>=5 && 5<7 && 7!=20 15==15==15==15==15 …
9 code-golf  parsing  conversion  syntax  code-golf  sequence  primes  code-challenge  geometry  optimization  code-golf  graph-theory  code-golf  number-theory  primes  integer  code-golf  source-layout  cops-and-robbers  code-golf  source-layout  cops-and-robbers  code-golf  sequence  primes  integer  code-golf  math  number-theory  primes  rational-numbers  code-golf  math  sequence  number-theory  primes  code-golf  string  code-golf  math  combinatorics  permutations  restricted-complexity  code-golf  array-manipulation  code-golf  number  sequence  code-golf  number  sequence  code-golf  binary-matrix  code-golf  math  tips  javascript  algorithm  code-golf  string  code-golf  number  sequence  code-golf  math  arithmetic  parsing  code-golf  number  sequence  primes  code-golf  string  ascii-art  geometry  integer  code-golf  geometry  code-golf  number  array-manipulation  code-golf  math  geometry  code-golf  number  sequence  arithmetic  integer  code-golf  string  kolmogorov-complexity  code-golf  number  code-golf  number  chess  code-golf  sequence  decision-problem  subsequence  code-golf  math  number  primes  code-golf  primes  permutations  code-golf  integer  probability-theory  statistics  code-golf  string  code-golf  sequence  decision-problem  parsing  board-game  code-golf  binary  graph-theory  code-golf  board-game  classification  tic-tac-toe  code-golf  ascii-art  polyglot  code-golf  date  code-golf  geometry 

30
给定int输入n,输出n * reversed(n)
给定一个整数n,打印输出n * reversed(n) reversed(n)是当您reverse的数字为时得到的数字n。 reverse(512) = 215 reverse(1) = 1 reverse(101) = 101 >>>>>>>> func(5) = 5*5 = 25 func(12) = 12*21 = 252 func(11) = 11*11 = 121 func(659) = 659*956 = 630004 最短的代码胜出! 排行榜 显示代码段 var QUESTION_ID=144816,OVERRIDE_USER=71625;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var …
9 code-golf  math  arithmetic  code-golf  math  integer  code-golf  arithmetic  integer  code-golf  sequence  base-conversion  palindrome  code-golf  math  primes  integer  code-golf  parsing  conversion  syntax  code-golf  sequence  primes  code-challenge  geometry  optimization  code-golf  graph-theory  code-golf  number-theory  primes  integer  code-golf  source-layout  cops-and-robbers  code-golf  source-layout  cops-and-robbers  code-golf  sequence  primes  integer  code-golf  math  number-theory  primes  rational-numbers  code-golf  math  sequence  number-theory  primes  code-golf  string  code-golf  math  combinatorics  permutations  restricted-complexity  code-golf  array-manipulation  code-golf  number  sequence  code-golf  number  sequence  code-golf  binary-matrix  code-golf  math  tips  javascript  algorithm  code-golf  string  code-golf  number  sequence  code-golf  math  arithmetic  parsing  code-golf  number  sequence  primes  code-golf  string  ascii-art  geometry  integer  code-golf  geometry  code-golf  number  array-manipulation  code-golf  math  geometry  code-golf  number  sequence  arithmetic  integer  code-golf  string  kolmogorov-complexity  code-golf  number  code-golf  number  chess  code-golf  sequence  decision-problem  subsequence  code-golf  math  number  primes  code-golf  primes  permutations  code-golf  integer  probability-theory  statistics  code-golf  string  code-golf  sequence  decision-problem  parsing  board-game  code-golf  binary  graph-theory  code-golf  board-game  classification  tic-tac-toe  code-golf  ascii-art  polyglot  code-golf  date  code-golf  geometry 

15
实现欧拉方法
这个挑战的目标是使用欧拉方法来近似形式为f (n)(x)= c的微分方程的解。† 输入将是一个整数列表,其中第n个值代表f (n)(0)的值。第一个整数是f(0),第二个整数是f'(0),依此类推。此列表中的最后一个整数是常数,并且将始终保持不变。 作为输入还提供了一个正(非零)整数x,它表示目标值(您正在尝试估计f(x))。欧拉方法的步长始终为1。因此,您将总共需要执行x步。 如果你是unfamliar欧拉的方法,这里是与输入的解释一个详细的例子[4, -5, 3, -1],X = 8。 x f(x) f'(x) f''(x) f'''(x) 0 4 -5 3 -1 1 4-5 = -1 -5+3 = -2 3-1 = 2 -1 2 -1-2 = -3 -2+2 = 0 2-1 = 1 -1 3 -3+0 = -3 0+1 = …

11
创建变量组的每个组合,最高可达n
规格 给定m变量,按order创建每个组合n。例如, 将两个变量(a和b)映射到顺序的输出为1: 一个 b b 将两个变量(a和b)映射到顺序的输出为2: 一个 一个2 b b 2 b 一个2 b 抗体2 a 2 b 2 将两个变量(a和b)映射到顺序的输出为3: 一个 一个2 一个3 b b 2 b 3 b 一个2 b 一个3 b a 3 b 2 抗体2 腹3 a 2 b 3 a 2 b 2 a 3 …

2
一百个最常用的单词
之所以写这个问题,是因为“ Thing Explainer”很有趣,并且给了我一个主意。 编写“使计算机做事的东西”,以读取/获取一组字母,数字和类似的东西"#%|?,True / 1如果所有单词都属于该集合,则返回。 如果所有单词都不属于该集合,请返回不属于该集合的单词。 在任何情况下都可以认为本网站是正确的。编写规则是为了遵守该站点上的规范。 例子: Truthy: 如果将第一条水平线上方的整个文本粘贴为输入,则代码应返回真实值。 以下几行应返回真实值(输入之间用分隔###) This returns "Hello, World!" ### tHiS rEtUrNs TrUe... ### Thing Explainer is a book written by a man. The man writes books with simple words. ### This set of stuff "#!^{>7( must return true 虚假的: 在以下示例中,输入和输出用分隔***。不同的测试用例用分隔###。 This code …
9 code-golf  string  parsing  code-golf  array-manipulation  random  permutations  code-golf  string  code-golf  parsing  code-golf  string  quine  code-golf  string  parsing  comment  code-golf  string  counting  natural-language  code-golf  string  decision-problem  code-golf  math  code-challenge  metagolf  test-battery  code-golf  string  code-golf  math  number  arithmetic  source-layout  code-golf  number  primes  decision-problem  code-golf  math  arithmetic  code-golf  date  code-golf  string  cryptography  code-golf  code-golf  chess  board-game  code-golf  geometry  grid  puzzle-solver  set-partitions  code-golf  number  natural-language  code-golf  ascii-art  code-golf  math  code-golf  string  ascii-art  kolmogorov-complexity  code-golf  string  natural-language  code-golf  game  board-game  tic-tac-toe  code-golf  ascii-art  hexagonal-grid  code-golf  string  comment  code-golf  internet  code-golf  sorting  code-golf  kolmogorov-complexity  unicode  code-golf  tips  code-golf  string  natural-language  code-golf  string  kolmogorov-complexity  source-layout  hello-world  code-golf  string  kolmogorov-complexity  counting  natural-language  code-golf  random  quine  code-golf  string  math  bitwise  code-golf  permutations  code-golf  string  code-golf  arithmetic 

7
塞克斯的序列
定义 a(1) = 1 a(2) = 2 a(n)是最小的数字k>a(n-1),它避免的任何3项算术级数a(1), a(2), ..., a(n-1), k。 换句话说,a(n)是的最小数目k>a(n-1),使得不存在x,y其中0<x<y<n和a(y)-a(x) = k-a(y)。 算出例子 对于n=5: 我们有 a(1), a(2), a(3), a(4) = 1, 2, 4, 5 如果为a(5)=6,则2, 4, 6形成算术级数。 如果为a(5)=7,则1, 4, 7形成算术级数。 如果为a(5)=8,则2, 5, 8形成算术级数。 如果为a(5)=9,则1, 5, 9形成算术级数。 如果为a(5)=10,则找不到算术级数。 因此a(5)=10。 任务 给定n,输出a(n)。 眼镜 n 将是一个正整数。 您可以使用0索引而不是1索引,在这种情况下n可以为0。如果您使用的是0索引,请在答案中注明。 计分 由于我们试图避免3项算术级数,并且3是一个小数,因此就字节数而言,您的代码应尽可能小(即短)。 测试用例 …

10
从一组现有的权重中进行选择以得出目标总和
举重时,我想通过将几块板连接到一根杆上来达到特定的重量。 我有以下板块: 6个每块1公斤的盘子 6盘2.5公斤装 6盘5公斤装 6盘10公斤装 酒吧本身重10公斤。 只允许成对安装板-将板连接在钢筋的两端,并且两端的布置必须完全对称(例如,在一端连接两个5公斤重的盘子,在一端连接10公斤重的盘子)出于安全考虑,另一端被禁止)。 编写一个程序或函数,告诉我要获得给定的总重量,我必须使用每种类型的板数。输入为大于11的整数。输出是一个包含4个数字的列表/数组/字符串。如果不可能合并现有板块以获得目标重量,则输出零/空数组,无效字符串,引发异常等。 如果有多种解决方案,则代码只能输出一种(不要让用户选择-他忙于其他事情)。 测试用例: 12 -> [2 0 0 0] - 2 plates of 1 kg plus the bar of 10 kg 13 -> [0 0 0 0] - a special-case output that means "impossible" 20 -> [0 0 2 0] - 2 …

3
简化日期
这类似于简化分数,但带有日期! 程序的输入必须采用以下格式mm/dd : 3/4 //March 4 12/15 //December 15 1/1 // January 1 我们假定输入将是有效的,以使月份中包含以下天数: January 31 February 28 March 31 April 30 May 31 June 30 July 31 August 31 September 30 October 31 November 30 December 31 程序的工作是采用假定的有效输入,并迭代(或递归)简化日期,并在每次迭代(包括第0次)时,输出日期,并带有上面写的月份的全名。 例如: 输入以下内容: 12/18 将输出 December 18 June 9 February 3 已经简化的输入仅输出自身: …

6
计算两个矩阵的克罗内克总和
在下面的例子中,A和B将是2×2矩阵,和矩阵是一个索引。 一个克罗内克产品具有以下特性: A⊗B = A(1,1)*B A(1,2)*B A(2,1)*B A(2,2)*B = A(1,1)*B(1,1) A(1,1)*B(1,2) A(1,2)*B(1,1) A(1,2)*B(1,2) A(1,1)*B(2,1) A(1,1)*B(2,2) A(1,2)*B(2,1) A(1,2)*B(2,2) A(2,1)*B(1,1) A(2,1)*B(1,2) A(2,2)*B(1,1) A(2,2)*B(1,2) A(2,2)*B(2,1) A(2,2)*B(1,2) A(2,2)*B(2,1) A(2,2)*B(2,2) 一个Kronecker和具有以下特性: A⊕B = A⊗Ib + Ia⊗B Ia和Ib是单位矩阵与的尺寸A和B分别。A并且B是平方矩阵。请注意,A并且B可以具有不同的大小。 A⊕B = A(1,1)+B(1,1) B(1,2) A(1,2) 0 B(2,1) A(1,1)+B(2,2) 0 A(1,2) A(2,1) 0 A(2,2)+B(1,1) B(1,2) 0 A(2,1) B(2,1) A(2,2)+B(2,2) …

3
验证出生号码
挪威出生号码由11位数字组成,组成方式如下: DDMMYYiiikk DD 是一天(从01-31开始) MM 是月份(从01到12) YY是年份(从00-99)。1900和2000之间没有区别 iii 是“个人号码” kk 是两个控制数字 iii 由出生年份和性别决定,方法如下 0000-1900:无视,存在一些不一致和特殊情况 1900-1999:范围= 000-499 2000-2039:范围= 500-999 女:偶数(和000) 男:奇数 控制编号是通过以下方式确定的: 我们称这11位数字为: d1 d2 m1 m2 y1 y2 i1 i2 i3 k1 k2 然后可以使用以下公式计算控制位数: k1 = 11 - ((3 * d1 + 7 * d2 + 6 * m1 + …

2
交替求解数学表达式
创建一个程序,使用表达式交替两边的元素来求解数学表达式。这样做的方法是,先阅读第一个字符,然后阅读最后一个,然后阅读第二个字符,再阅读第二个字符,再读另一个字符,而不是从左到右阅读。这将为您提供一个新的表达式,您必须对其进行计算和输出。 a*b/c+d-e 135798642 <-- Order you read the expression in ae*-bd/+c <-- Order of operation. 例: 1*3/2+4-5 15*-34/+2 = -255 如果表达式不起作用,则1必须将其插入必要的位置以使其起作用。 一些示例可能会更好地说明它: Input: 1+1+1+1+1 Result: 23 // Because 1+1+1+1+1 -> 11++11++1 -> 23 Input: 1+2-3+12-5 Result: -19 // Because 1+2-3+12-5 -> 15+-22-13+ -> 15+-22-13+1 -> -19 // | // Not …

6
计算克罗内克符号
这里和这里的相关链接,但是这里是简短的版本: 您有两个整数的输入,a且b介于负无穷大和无穷大之间(尽管如有必要,我可以限制范围,但函数仍必须接受负无穷大)。 Kronecker符号的定义 您必须返回Kronecker符号(a|b)进行输入,a并且b在哪里 (a|b) = (a|p_1)^e_1 * (a|p_2)^e_2 * ... * (a|p_n)^e_n 在那里b = p_1^e_1 * p_2^e_2 * ... * p_n^e_n,并p_i与e_i在的因式分解素数和指数b。 对于奇素数p,(a|p)=a^((p-1)/2) (mod p)定义在这里。 对于b == 2,(n|2)={0 for n even; 1 for n odd, n=+/-1 (mod 8); -1 for n odd, n=+/-3 (mod 8) 对于b == -1,(n|-1)={-1 for n<0; …

2
遗传基础变化
背景 在这一挑战,一个碱基b表示的整数的n是的表达n作为功率的总和b,其中每个术语最多出现b-1次数。例如,的基本4表示2015是 4^5 + 3*4^4 + 3*4^3 + 4^2 + 3*4 + 3 现在,遗传碱基b的表示n是通过将指数纳入其碱基获得的b陈述,然后将它们的指数,等等递归。因此,的遗传基础4表示2015为 4^(4 + 1) + 3*4^4 + 3*4^3 + 4^2 + 3*4 + 3 作为一个更复杂的例子中,遗传碱基3的表示 7981676788374679859068493351144698070458 是 2*3^(3^(3 + 1) + 2) + 3 + 1 从到的遗传基数变化(nbc表示为H(b, c, n))是通过获取的遗传基数b表示形式n,用bby 替换每一个c并评估结果表达式而获得的数字。例如, H(3, 2, 7981676788374679859068493351144698070458) 是 2*2^(2^(2 + 1) …

15
不转换的字符串总和
您必须接受2个字符串输入并输出这两个字符串的总和,而无需将它们转换为int或使用任何数字数据类型。 例如 string one = "123"; string two = "456"; string sum = "579"; 假设字符串不能超过10位数字。 这是代码高尔夫,最短的答案是角色获胜。对C#中答案的编辑会使我微笑:)。 编辑:转换为int可以定义为这种性质的任何东西 Int.TryParse,(int),Convert.ToInt等
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.