c语言与c数学有关吗?

今天主要介绍C语言关于数学函数几个的相关操作。我用的编译器是tcc。实在没必要用一些大型的编译器,tcc小巧实用,实为居家旅行必备之神器!
1.求任意两个数之间的素数,我加了一个素数判断的函数,很实用。code:
1 /*求任意两数之间的素数*/
2 /*long 为 2^31-1=2,147,483,647*/
3 #include &stdio.h&
4 #include &math.h&
/*double sqrt(double)*/
5 int isPrime(long num);
6 void AtoBPrime(long n1,long n2);
7 int main()
9    if(isPrime(102))
     printf("Prime");
    printf("Not Prime!");
17    AtoBPrime(100,10000);
  return 0;
20 /*判断是否是素数,1-是素数,0-不是素数,输入为任意整数*/
21 int isPrime(long num)
23    int flag = 0;
24    int mov = 2;
  if(num &= 1)
28       flag = 0;
30    if(2 == num)
  flag = 1;
  high = (long)sqrt(num);
  for(; mov& mov++)
   if(num % mov == 0)
  flag = 0;
   mov =
  else if(high != mov+1)
   continue;
  return
54 /*求任意两个数之间的素数*/
55 void AtoBPrime(long n1,long n2)
int cnt=0;
printf("the array prime:\n");
for(mov=n1;mov&n2;mov++)
if(isPrime(mov))
if(cnt%5 == 0)
printf("\n");
printf("%d ",mov);
2.回文数。
1 /*Palindrome:回文数,指正读,反读都是一样的数*/
2 #include &stdio.h&
3 #include &stdlib.h& /*char* ltoa (long, char*, int)*/
4 #include &string.h& /*size_t strlen(char *)*/
5 #include &math.h&
6 int isPalindrome(long num);
7 void AtoBPalindrome(long n1,long n2);
8 int main()
if(isPalindrome(101))
printf("Palindrome\n");
printf("Not Palindrome!\n");
AtoBPalindrome(10,10000);
21 /*判断是否是素数,1-是回文数,0-不是回文数,输入为任意整数*/
22 int isPalindrome(long num)
int i,len,
int flag=0;
char arr[20];
/*字符型数组,存放数的各个位上的数字*/
ltoa(num,arr,10);
/***************************
char* ltoa (long, char*, int);
long:指定装换的长整形数字
char*:接受字符串
int:进制属性
****************************/
len = strlen(arr);
half = len/2;
for(i=0;i&=i++)
if(arr[i] != arr[--len])
if(i &= half)
54 /*打印三层回文数*/
55 void AtoBPalindrome(long n1,long n2)
for(mov=n1 ; mov&n2 ;mov++)
if(isPalindrome(mov)&&isPalindrome(mov*mov)&&isPalindrome(mov*mov*mov))
printf("n=%d\n",mov);
3.神奇的6174
1 /*神奇6174,找到一个四位数,从大到小排列和从小到大排列所得数的差为它自己*/
2 #include &stdio.h&
3 #include &stdlib.h&
4 #include &string.h&
5 #include &math.h&
6 int isSame(long num);
7 void AtoBSame(long n1,long n2);
8 int main()
AtoBSame(1000,10000);
13 int isSame(long num)
int flag = 0;
int arr[4];
int i=0,j,
int n1,n2;//重新组合数字
/*解析数字*/
arr[0] = num/1000;
arr[1] = num/100%10;
arr[2] = num/10%10;
arr[3] = num%10;
/*排序,从大到小*/
for(;i&3;i++)
for(j=i+1;j&4;j++)
if(arr[i]&arr[j])
temp = arr[j];
arr[j] = arr[i];
n1 = arr[0]*1000+arr[1]*100+arr[2]*10+arr[3];
n2 = arr[3]*1000+arr[2]*100+arr[1]*10+arr[0];
/*两个数是相差是否是一个数*/
if(num == n1-n2)
47 /*求能够满足条件的特殊数*/
48 void AtoBSame(long n1,long n2)
for(mov=n1;mov&n2;mov++)
if(isSame(mov))
printf("num=%d\n",mov);
4.数字e,与最大公约数,最小公倍数。
1 /*实用的数字操作*/
2 #include &stdio.h&
3 #include &math.h&
4 float efun();/*求的数字e*/
5 long divisor(long n1,long n2);/*最大公约数*/
6 long multiple(long n1,long n2);/*最小公倍数*/
7 int main()
float e=0.0f;
e = efun();
printf("%8f\n",e);
/*最大公约数与最小公倍数测试语句*/
d = divisor(9,100);
m = multiple(10,101);
printf("d=%d\nm=%d",d,m);
20 float efun()
float e=1.0f,n=1.0f;
int i = 1;
while(1/n & 1e-10)
32 /*求最大公约数函数*/
33 long divisor(long n1,long n2)
long t,c,d;
/*说明n1比较大*/
/*求最大公约数*/
c = n1%n2;
while(c != 0)
c = n1%n2;
d = n2;/*d为最大公约数*/
54 /*求小公倍数函数*/
55 long multiple(long n1,long n2)
c = n1*n2;
m = c/divisor(n1,n2);
5.牛顿法解方程。
1 /*牛顿法解方程*/
2 #include &stdio.h&
3 #include &math.h&
/*fabs(float)*/
4 float Newton(float a , float b , float c , float d);
5 int main()
x = Newton(1,2,3,4);
printf("x=%.5f\n",x);
12 /*牛顿解法函数形式*/
13 float Newton(float a , float b , float c , float d)
float x=1.0f,x0,f,f1;
f = ((a*x0+b)*x0+c)*x0+d;
/*函数本身*/
f1 = (3*a*x0+2*b)*x0+c;
x = x0 - f/f1;
/*迭代公式*/
}while(fabs(x-x0) &= 1e-5);
6.计算系统运行时间
/*计算系统运行时间*/
#include &stdio.h&
#include &conio.h&/*kbhit(void)*/
/*********************************************
kbhit(void)
功 能及返回值:检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用 法:int kbhit(void);
*********************************************/
#include &stdlib.h&/*void _sleep (unsigned long);tcc*/
typedef struct time
int main()
time t = {0,0,0};
while(!kbhit())
_sleep(1000);/*1s=1000ms*/
if(t.second == 59)
t.minute = t.minute + 1;
if(t.minute == 60)
t.hour = t.hour+1;
t.minute = 0;
t.second = 0;
t.second = t.second+1;
printf("%d:%d:%d\n",t.hour,t.minute,t.second);
阅读(...) 评论()君,已阅读到文档的结尾了呢~~
编数学程序c语言和matlab
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
编数学程序c语言和matlab有什么区别?
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 c语言与c 的文章

 

随机推荐