【问题标题】:autohotkey: add consecutive range of numbersautohotkey:添加连续的数字范围
【发布时间】:2016-04-11 18:38:13
【问题描述】:

我正在尝试在 Autohotkey 中添加一系列连续的奇数 (13-1001)。

是否有一个公式可以解决这个问题?

这就是我所拥有的:

a:=13
b:=1001
s1:=((b+1)/2)**2
s2:=((a-1)/2)**2
s:=s1-s2
S3:=z+s
Msgbox,
(   
Step 6 Results:
Z is %z%
First # is %a%
Last # is %b%
Sum of consecutive odd numbers (13-1001) is %s%
Z+Sum is %s3%

【问题讨论】:

  • z 是从哪里来的,这个公式和 Autohotkey 有什么关系?
  • 我正在回答一系列问题以帮助我了解有关 Autohotkey 的更多信息。最初的问题是:“将 13-1001(含)的所有奇数加到 Z”Z 来自上一个问题。 Z=3
  • 您在处理示例时遇到了哪些问题?显示的代码与问题有何关系?请详细说明。
  • 我想让这段代码更简洁,并想知道是否有另一个函数可以用更少的行来完成。我正在尝试下面的表达式,但它在 Autohotkey 中不起作用:add(range(13,1001,2))

标签: math range autohotkey addition


【解决方案1】:

转发

这个逻辑假设如下:

  • Start 小于 End
  • StartEnd 都是奇数

原始示例

Start := 13
, End := 1001

, SumOfOddNumbers := (((End + 1)^2)/4) - ((Start - 1)^2)/4)) 

MsgBox, % "Sum of all odd numbers from " . Start . " to " . End . " is " . SumOfOddNumbers

输出

Sum of all odd numbers from 13 to 1001 is 250965

作为一个函数

funSumOfOddNumbers( Start, End) {
    Temp := (((End + 1)^2)/4) - ((Start - 1)^2)/4)) 
    return, % Temp
    } ; end function funSumOfOddNumbers

MsgBox, % "Sum of all odd numbers from 49 to 4009 is " . funSumOfOddNumbers(49, 4009)

输出

Sum of all odd numbers from 49 to 4009 is 4019449

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2017-01-30
    相关资源
    最近更新 更多