导航:首页 > 软件知识 > 如何把算法做成小程序

如何把算法做成小程序

发布时间:2024-07-09 09:55:19

❶ 怎样用Python语言编一个小程序

编写 Python 小程序的皮册方法燃握宏主要分为以下几步:

安装 Python:在编写 Python 程序之前,需要在计算机上安装 Python。Python 官网提供了下载安装程序皮辩的链接,可以根据操作系统版本下载安装程序。

编写代码:可以使用任何文本编辑器编写 Python 代码。代码的具体内容根据程序的需求来决定,可以包括各种 Python 原生语法、内置函数、第三方库等等。

运行程序:可以使用 Python 解释器来运行 Python 程序。在终端或命令行界面输入 python 文件名.py 即可执行程序。

下面是一个简单的示例程序:

❷ 缂栦竴涓绠鍗旷殑C璇瑷灏忕▼搴忋伞伞伞伞傚叧浜嶳SA绠楁硶镄

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//RSA??????
typedef struct RSA_PARAM_Tag
{
unsigned __int64 p, q; //????,?????????
unsigned __int64 f; //f=(p-1)*(q-1),?????????
unsigned __int64 n, e; //??,n=p*q,gcd(e,f)=1
unsigned __int64 d; //??,e*d=1 (mod f),gcd(n,d)=1
unsigned __int64 s; //??,??2^s<=n????s,?log2(n)
} RSA_PARAM;

//????
const static long g_PrimeTable[]=
{
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97
};

const static long g_PrimeCount=sizeof(g_PrimeTable) / sizeof(long);const unsigned __int64 multiplier=12747293821;
const unsigned __int64 adder=1343545677842234541;//????

typedef struct _RandNumber
{
/* */
unsigned __int64 randSeed;/* */
}RandNumber;

static void CreateRandNumber(RandNumber *pRand, unsigned __int64 s);
static unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n);

/* */
void CreateRandNumber(RandNumber *pRand, unsigned __int64 s)
{
if(!s)
{
pRand->randSeed= (unsigned __int64)time(NULL);
}
else
{
pRand->randSeed=s;
}
}

/* */
unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n)
{
pRand->randSeed=multiplier * pRand->randSeed + adder;
return pRand->randSeed % n;
}

static RandNumber g_Rnd;

/*
????,??? x=a*b mod n
*/
unsigned __int64 MulMod(unsigned __int64 a, unsigned __int64 b, unsigned __int64 n)
{
return a * b % n;
}

/*
????,??? x=base^pow mod n
*/
unsigned __int64 PowMod(unsigned __int64 base, unsigned __int64 pow, unsigned __int64 n)
{
unsigned __int64 a=base, b=pow, c=1;
while(b)
{
while(!(b & 1))
{
b>>=1; //a=a * a % n; //?????????64????,?????a*a?a>=2^32????????,??????????64?
a=MulMod(a, a, n);
}

b--; //c=a * c % n; //??????,??64???????32????????????????
c=MulMod(a, c, n);
}
return c;
}

/*
Rabin-Miller????,??????1,????0?
n??????
??:????????????,???????????1/4
*/
long RabinMillerKnl(unsigned __int64 n)
{
unsigned __int64 b, m, j, v, i;
unsigned __int64 TWO = 2;
m=n - 1;
j=0; //0?????m?j,??n-1=m*2^j,??m????,j?????
while(!(m & 1))
{
++j;
m>>=1;
}

//1??????b,2<=b<n-1
b=2 + Random(&g_Rnd, n - 3);

//2???v=b^m mod n
v=PowMod(b, m, n);

//3???v==1,????
if(v == 1)
{
return 1;
}

//4??i=1
i=1;

//5???v=n-1,????
while(v != n - 1)
{
//6???i==l,???,??
if(i == j)
{
return 0;
}

//7?v=v^2 mod n,i=i+1
v=PowMod(v, TWO, n);
++i;

//8????5
}

return 1;
}

/*
Rabin-Miller????,??????loop?
??????1,????0
*/
long RabinMiller(unsigned __int64 n, long loop)
{
long i=0;

//?????????,????
for(i=0; i < g_PrimeCount; i++)
{
if(n % g_PrimeTable[i] == 0)
{
return 0;
}
}

//????Rabin-Miller??loop?,??????????????(1/4)^loop
for(i=0; i < loop; i++)
{
if(!RabinMillerKnl(n))
{
return 0;
}
}

return 1;
}

/*
??????bits?(????)???,??32?
*/
unsigned __int64 RandomPrime(char bits)
{
unsigned __int64 base;
do
{
base= (unsigned long)1 << (bits - 1); //??????1
base+=Random(&g_Rnd, base); //????????
base|=1; //??????1,??????
} while(!RabinMiller(base, 30)); //????-????30?
return base; //?????????
}

/*
???????????
*/
unsigned __int64 EuclidGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t;
if(p == q)
{
return p; //????,?????????
}
else
{
while(b) //?????,gcd(a,b)=gcd(b,a-qb)
{
a=a % b;
t=a;
a=b;
b=t;
}

return a;
}
}

/*
Stein???????
*/
unsigned __int64 SteinGcd(unsigned __int64 p, unsigned __int64 q)
{
unsigned __int64 a=p > q ? p : q;
unsigned __int64 b=p < q ? p : q;
unsigned __int64 t, r=1;
if(p == q)
{
return p; //????,?????????
}
else
{
while((!(a & 1)) && (!(b & 1)))
{
r<<=1; //a?b?????,gcd(a,b)=2*gcd(a/2,b/2)
a>>=1;
b>>=1;
}

if(!(a & 1))
{
t=a; //??a???,??a,b
a=b;
b=t;
}

do
{
while(!(b & 1))
{
b>>=1; //b???,a????,gcd(b,a)=gcd(b/2,a)
} if(b < a)
{
t=a; //??b??a,??a,b
a=b;
b=t;
} b=(b - a) >> 1; //b?a????,gcd(b,a)=gcd((b-a)/2,a)
} while(b);

return r * a;
}
}

/*
??a?b,?x,??a*x =1 (mod b)
?????a*x-b*y=1??????
*/
unsigned __int64 Euclid(unsigned __int64 a, unsigned __int64 b)
{
unsigned __int64 m, e, i, j, x, y;
long xx, yy;
m=b;
e=a;
x=0;
y=1;
xx=1;
yy=1;
while(e)
{
i=m / e;
j=m % e;
m=e;
e=j;
j=y;
y*=i;
if(xx == yy)
{
if(x > y)
{
y=x - y;
}
else
{
y-=x;
yy=0;
}
}
else
{
y+=x;
xx=1 - xx;
yy=1 - yy;
} x=j;
} if(xx == 0)
{
x=b - x;
} return x;
}

/*
??????RSA????
*/
RSA_PARAM RsaGetParam(RandNumber Rnd)
{
RSA_PARAM Rsa={ 0 };
unsigned __int64 t;
Rsa.p=RandomPrime(16); //????????
Rsa.q=RandomPrime(16);
Rsa.n=Rsa.p * Rsa.q;
Rsa.f=(Rsa.p - 1) * (Rsa.q - 1);
do
{
Rsa.e=Random(&Rnd, 65536); //??2^16,65536=2^16
Rsa.e|=1; //??????1,??????,?f?????,???,?????
} while(SteinGcd(Rsa.e, Rsa.f) != 1);

Rsa.d=Euclid(Rsa.e, Rsa.f);
Rsa.s=0;
t=Rsa.n >> 1;
while(t)
{
Rsa.s++; //s=log2(n)
t>>=1;
}

return Rsa;
}

/*
??-????
*/
void TestRM(void)
{
unsigned long k=0;
unsigned __int64 i = 0;

printf(" - Rabin-Miller prime check.\n\n");

for(i=4197900001; i < 4198000000; i+=2)
{
if(RabinMiller(i, 30))
{
k++;
printf("%ul\n", i);
}
}

printf("Total: %ul\n", k);
}

void Usage(void)
{
printf("0. exit\n");
printf("1. encryption\n");
printf("2. decrypt\n");
printf("enter your choice:");
}

int GetChoice(void)
{
char s[80] = {0};
fgets(s, sizeof(s), stdin);

if ('0' > s[0] || '9' < s[0] || '\n' != s[1])
{
return -1;
}
else
{
return s[0] - '0';
}
}

#define ENCRYPT_CODE 1
#define DECRYPT_CODE 2
#define EXIT_CODE 0

#define MAX_SIZE 1024

int main(void)
{
RSA_PARAM r;
char pSrc[MAX_SIZE]={0};
unsigned long n = 0;
unsigned char *q = NULL;
unsigned char pDec[MAX_SIZE] = {0};
unsigned __int64 pEnc[MAX_SIZE] = {0};
unsigned __int64 tmp = 0;
unsigned long i=0;
int choice = 0;

unsigned __int64 s = 0;
CreateRandNumber(&g_Rnd, s);

r=RsaGetParam(g_Rnd);

/*
printf("p=%ul\n", r.p);
printf("q=%ul\n", r.q);

printf("f=(p-1)*(q-1)=%ul\n", r.f);
printf("n=p*q=%ul\n", r.n);
printf("e=%ul\n", r.e);
printf("d=%ul\n", r.d);
printf("s=%ul\n", r.s);
*/

while (1)
{
memset(pSrc, 0, MAX_SIZE);
memset(pDec, 0, MAX_SIZE);
memset(pEnc, 0, MAX_SIZE);

Usage();

choice = GetChoice();
switch(choice)
{
case ENCRYPT_CODE:
printf("Source:%s", pSrc);
scanf("%s", pSrc);
n = strlen(pSrc);
getchar();

q= (unsigned char *)pSrc;
printf("Encode:");

for(i = 0; i < n; i++)
{
tmp = q[i];
pEnc[i]=PowMod(tmp, r.e, r.n);
printf("%x ", pEnc[i]);
}
printf("\n");
break;

case DECRYPT_CODE:
printf("Enter length of ciphertext:");
scanf("%d", &n);
getchar();

for(i=0; i < n; i++)
{
printf("Enter ciphertext(%d):", i+1);
scanf("%x", &pEnc[i]);
getchar();
}

printf("Decode:");
for(i=0; i < n; i++)
{
pDec[i]=PowMod(pEnc[i], r.d, r.n);
printf("%x ", (unsigned long)pDec[i]);
}
printf("\n");
printf("%s\n", (char *)pDec);
break;

case EXIT_CODE:
break;
default:
break;
}

if (EXIT_CODE == choice)
{
break;
}
else
{
continue;
}
}

return 0;
}

阅读全文

与如何把算法做成小程序相关的资料

热点内容
玻璃上打印小圆点是什么技术 浏览:407
公式如何追随数据更新 浏览:55
电脑新增用户怎么设置程序 浏览:819
股市的左侧交易和右侧交易指什么 浏览:800
基金交易产生的费用有哪些 浏览:75
如何设计产品众筹 浏览:255
产品前期都是怎么运维的 浏览:658
张北新市场在什么地方 浏览:676
丰田卡罗拉系统程序怎么设置 浏览:468
属于衍生金融产品的是哪些 浏览:151
朵尚代理怎么样 浏览:530
抖音主页怎么把小程序去掉 浏览:211
交易猫九游账号怎么交易 浏览:913
北理测控技术怎么样 浏览:222
遵义有哪些菜市场可以摆地摊 浏览:371
怎么取消微信的小程序 浏览:102
推特别人可以看到自己哪些信息 浏览:360
怎么判断是否原厂数据线 浏览:511
蜗牛信息怎么设置 浏览:892
物联网应用技术有哪些 浏览:161