我试图了解如何使用 mod 运算符重复循环。
如果有两个字符串"abc"和"defgh",如何%用于循环,重复直到到达abc结尾?defgh即abc和的长度的模关系是什么defgh?
我不太明白这个概念。
例如:
x = (y*6) % 26 #this is a consistent formula
18 = (y*6) % 26
Run Code Online (Sandbox Code Playgroud)
我该如何解决y?或者有没有办法轻松反转 % 操作?
通过一个教程,我了解到模函数返回等式的余数。因此,例如,3 % 4等于 3
但我似乎不明白如何25 * 3 % 4 = 3。25 怎么了?
我已经在 PowerShell 以及在线 Google 计算器上运行该脚本,返回相同的结果。有谁愿意解释一下吗?
目前,我正在学习科特林并试图创建在所有工作的延伸(中缀)方法号码类型(Byte,Long,Float等)。它应该像 Python 的%运算符一样工作:
4 % 3 == 1 // only this is the same as Java's %
4 % -3 == -2
-4 % 3 == 2
-4 % -3 == -1
Run Code Online (Sandbox Code Playgroud)
...或者像 Java 的Math.floorMod,但它也应该与Doubleor 一起使用Float:
-4.3 % 3.2 == 2.1000000000000005
Run Code Online (Sandbox Code Playgroud)
或这些类型的任何可能组合
3 % 2.2 == 0.7999999999999998
3L % 2.2f == 0.7999999999999998
Run Code Online (Sandbox Code Playgroud)
以下按预期工作,但仅适用于两个Double或两个Int:
inline infix fun Double.fmod(other: Double): Number { …Run Code Online (Sandbox Code Playgroud) 编辑:
我有一个很大的数字,C 本身没有它的类型。我必须使用字符数组来保存它。作为示例,我创建了一个 32 字节数组。它代表一个大数,最大可达 2 ^ 256。
unsigned char num[32]; // The size could be any number for this question.
Run Code Online (Sandbox Code Playgroud)
我想对其进行模运算,例如,我想用一个小除数对大数进行模运算并得到一个整数类型的结果。
int divisor = 1234; // Note that the divisor is much smaller than the big number
int result;
// do something here
// to produce a result
// like result = number mod divisor
Run Code Online (Sandbox Code Playgroud)
我不想使用其他库。我该怎么做?
我正在 go 中实现adler32 checksum的滚动版本。
这个答案有助于仔细检查我的数学。然而我很难在 golang 中正确实现它。
我写了以下代码:
func roll(adler, n, leave, enter uint32) uint32 {
a := adler & 0xffff
b := adler >> 16
a = (a + enter - leave) % MOD
b = (b - n*leave - 1 + a) % MOD
return b<<16 | a
}
Run Code Online (Sandbox Code Playgroud)
它在各种输入上进行了测试,并且运行良好,直到我决定在随机数据上运行它。这是一个不起作用的示例(我找到了其中几个)。
令我困惑的是,Python 中的相同代码在这些输入上完美运行:
def roll(adler, n, leave, enter):
a = adler & 0xffff
b = adler >> 16
a = (a + …Run Code Online (Sandbox Code Playgroud) 我希望在 PostgreSQL 中执行两个浮点整数之间的模运算。具体来说,我希望强制执行角度算术,例如在一些数学运算之后强制角度位于区间 [0,2*pi] 内。
我可以看到fmodSQL 中并未正式支持此类。有什么合理的解决办法吗?
我想显示 4 张图像,因此我使用 来unsigned short表示当前图像的索引。当我按A时,索引减1;当我按 D 时,索引增加 1。
image_index = (image_index - 1) % 4;我正在使用(以及 D 按键上的image_index = (image_index + 1) % 4;)计算 A 按键上的索引
如果我向前循环(即,按 D),一切都会按预期工作,但如果我位于索引 0 并按 A,它会下溢到无符号短整型的最大值,并且不会以 4 为模来给出索引3.
我知道对于有符号类型,上溢/下溢是 UB,但我确信对于无符号类型,它是定义良好的。有什么想法可能导致它忽略模数吗?
enum class Errors : short {
kSdlSuccess = 0,
kSdlInitFailure = -1,
kSdlWindowCreationError = -2,
kSdlRendererCreationError = -3
};
int main(int argc, char** argv) {
sdl::Context ctx;
sdl::Window window({ .w = 600, .h = 480, .flags = 0});
sdl::Renderer renderer(window, {0}); …Run Code Online (Sandbox Code Playgroud) 有人可以解释一下语义吗
n % 2 == 1
Run Code Online (Sandbox Code Playgroud)
和
n //= 2
Run Code Online (Sandbox Code Playgroud)
据我了解,检查byn % 2 == 1除法的余数是否为。n21
关于什么n //= 2?这是楼层划分吗?但什么?n除以2?
给定一个 1024 位模和几个长整数,我想找出其中哪个值是二次余数。
Legendre 符号应该返回 -1、0 或 1,但我的代码返回的值比这些值大几个数量级。
p = 101524035174539890485408575671085261788758965189060164484385690801466167356667036677932998889725476582421738788500738738503134356158197247473850273565349249573867251280253564698939768700489401960767007716413932851838937641880157263936985954881657889497583485535527613578457628399173971810541670838543309159139
ints = [25081841204695904475894082974192007718642931811040324543182130088804239047149283334700530600468528298920930150221871666297194395061462592781551275161695411167049544771049769000895119729307495913024360169904315078028798025169985966732789207320203861858234048872508633514498384390497048416012928086480326832803, 45471765180330439060504647480621449634904192839383897212809808339619841633826534856109999027962620381874878086991125854247108359699799913776917227058286090426484548349388138935504299609200377899052716663351188664096302672712078508601311725863678223874157861163196340391008634419348573975841578359355931590555, 17364140182001694956465593533200623738590196990236340894554145562517924989208719245429557645254953527658049246737589538280332010533027062477684237933221198639948938784244510469138826808187365678322547992099715229218615475923754896960363138890331502811292427146595752813297603265829581292183917027983351121325, 14388109104985808487337749876058284426747816961971581447380608277949200244660381570568531129775053684256071819837294436069133592772543582735985855506250660938574234958754211349215293281645205354069970790155237033436065434572020652955666855773232074749487007626050323967496732359278657193580493324467258802863, 4379499308310772821004090447650785095356643590411706358119239166662089428685562719233435615196994728767593223519226235062647670077854687031681041462632566890129595506430188602238753450337691441293042716909901692570971955078924699306873191983953501093343423248482960643055943413031768521782634679536276233318, 85256449776780591202928235662805033201684571648990042997557084658000067050672130152734911919581661523957075992761662315262685030115255938352540032297113615687815976039390537716707854569980516690246592112936796917504034711418465442893323439490171095447109457355598873230115172636184525449905022174536414781771, 50576597458517451578431293746926099486388286246142012476814190030935689430726042810458344828563913001012415702876199708216875020997112089693759638454900092580746638631062117961876611545851157613835724635005253792316142379239047654392970415343694657580353333217547079551304961116837545648785312490665576832987, 96868738830341112368094632337476840272563704408573054404213766500407517251810212494515862176356916912627172280446141202661640191237336568731069327906100896178776245311689857997012187599140875912026589672629935267844696976980890380730867520071059572350667913710344648377601017758188404474812654737363275994871, 4881261656846638800623549662943393234361061827128610120046315649707078244180313661063004390750821317096754282796876479695558644108492317407662131441224257537276274962372021273583478509416358764706098471849536036184924640593888902859441388472856822541452041181244337124767666161645827145408781917658423571721, 18237936726367556664171427575475596460727369368246286138804284742124256700367133250078608537129877968287885457417957868580553371999414227484737603688992620953200143688061024092623556471053006464123205133894607923801371986027458274343737860395496260538663183193877539815179246700525865152165600985105257601565]
def Legendre(arr,modulo):
exponent = ((modulo-1)/2)
for i in range(len(arr)):
candidate = int(pow(int(arr[i]),int(exponent),modulo))
print("{} : {}\n".format(candidate,arr[i]))
Legendre(ints,p)
Run Code Online (Sandbox Code Playgroud) modulo ×10
python ×4
math ×3
c ×2
adler32 ×1
c++ ×1
checksum ×1
cryptography ×1
for-loop ×1
go ×1
kotlin ×1
numbers ×1
postgresql ×1
python-2.7 ×1
python-3.x ×1
syntax ×1
underflow ×1