mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge pull request #3 from kvedala/project_euler/master2
Project euler/master2
This commit is contained in:
commit
40531e65c0
@ -13,12 +13,15 @@ include_directories(function_timer/include)
|
|||||||
# include_directories(${CMAKE_BINARY_DIR}/include)
|
# include_directories(${CMAKE_BINARY_DIR}/include)
|
||||||
# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
add_subdirectory(conversions)
|
add_subdirectory(conversions)
|
||||||
add_subdirectory(misc)
|
add_subdirectory(misc)
|
||||||
add_subdirectory(project_euler)
|
add_subdirectory(project_euler)
|
||||||
|
add_subdirectory(sorting)
|
||||||
|
add_subdirectory(searching)
|
||||||
|
add_subdirectory(numerical_methods)
|
||||||
|
|
||||||
if(USE_OPENMP)
|
if(USE_OPENMP)
|
||||||
find_package(OpenMP)
|
find_package(OpenMP)
|
||||||
|
22
DIRECTORY.md
22
DIRECTORY.md
@ -5,15 +5,6 @@
|
|||||||
* [Udp Client](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Client.c)
|
* [Udp Client](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Client.c)
|
||||||
* [Udp Server](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Server.c)
|
* [Udp Server](https://github.com/TheAlgorithms/C/blob/master/client_server/UDP_Server.c)
|
||||||
|
|
||||||
## Computer Oriented Statistical Methods
|
|
||||||
* [Gauss Elimination](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/Gauss_Elimination.c)
|
|
||||||
* [Lagrange Theorem](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/lagrange_theorem.C)
|
|
||||||
* [Mean](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/MEAN.C)
|
|
||||||
* [Median](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/MEDIAN.C)
|
|
||||||
* [Seidal](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/Seidal.C)
|
|
||||||
* [Simpson'S 1-3Rd Rule](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/simpson's%201-3rd%20rule.c)
|
|
||||||
* [Variance](https://github.com/TheAlgorithms/C/blob/master/computer_oriented_statistical_methods/variance.c)
|
|
||||||
|
|
||||||
## Conversions
|
## Conversions
|
||||||
* [Binary To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_decimal.c)
|
* [Binary To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_decimal.c)
|
||||||
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_hexadecimal.c)
|
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/C/blob/master/conversions/binary_to_hexadecimal.c)
|
||||||
@ -206,6 +197,16 @@
|
|||||||
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C/blob/master/misc/Tower_Of_Hanoi.c)
|
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C/blob/master/misc/Tower_Of_Hanoi.c)
|
||||||
* [Union Find](https://github.com/TheAlgorithms/C/blob/master/misc/union_Find.c)
|
* [Union Find](https://github.com/TheAlgorithms/C/blob/master/misc/union_Find.c)
|
||||||
|
|
||||||
|
## Numerical Methods
|
||||||
|
* [Gauss Elimination](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/Gauss_Elimination.c)
|
||||||
|
* [Lagrange Theorem](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/lagrange_theorem.C)
|
||||||
|
* [Mean](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/MEAN.C)
|
||||||
|
* [Median](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/MEDIAN.C)
|
||||||
|
* [Newton-Raphson-Root](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/newton-raphson-root.c)
|
||||||
|
* [Seidal](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/Seidal.C)
|
||||||
|
* [Simpsons 1-3Rd Rule](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/simpsons_1-3rd%20rule.c)
|
||||||
|
* [Variance](https://github.com/TheAlgorithms/C/blob/master/numerical_methods/variance.c)
|
||||||
|
|
||||||
## Project Euler
|
## Project Euler
|
||||||
* Problem 01
|
* Problem 01
|
||||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2001/sol1.c)
|
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2001/sol1.c)
|
||||||
@ -257,6 +258,8 @@
|
|||||||
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2023/sol2.c)
|
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2023/sol2.c)
|
||||||
* Problem 25
|
* Problem 25
|
||||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2025/sol1.c)
|
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2025/sol1.c)
|
||||||
|
* Problem 26
|
||||||
|
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2026/sol1.c)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
* [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)
|
* [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)
|
||||||
@ -296,4 +299,5 @@
|
|||||||
* [Selection Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Selection_Sort.c)
|
* [Selection Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Selection_Sort.c)
|
||||||
* [Shaker Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shaker_sort.c)
|
* [Shaker Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shaker_sort.c)
|
||||||
* [Shell Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_Sort.c)
|
* [Shell Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_Sort.c)
|
||||||
|
* [Shell Sort2](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_Sort2.c)
|
||||||
* [Stooge Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Stooge_Sort.c)
|
* [Stooge Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Stooge_Sort.c)
|
||||||
|
@ -1,66 +1,71 @@
|
|||||||
|
|
||||||
// Write CPP code here
|
// Write CPP code here
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#define MAX 80
|
#include <arpa/inet.h>
|
||||||
#define PORT 8080
|
#define MAX 80
|
||||||
#define SA struct sockaddr
|
#define PORT 8080
|
||||||
void func(int sockfd)
|
#define SA struct sockaddr
|
||||||
{
|
void func(int sockfd)
|
||||||
char buff[MAX];
|
{
|
||||||
int n;
|
char buff[MAX];
|
||||||
for (;;) {
|
int n;
|
||||||
bzero(buff, sizeof(buff));
|
for (;;)
|
||||||
printf("Enter the string : ");
|
{
|
||||||
n = 0;
|
bzero(buff, sizeof(buff));
|
||||||
while ((buff[n++] = getchar()) != '\n')
|
printf("Enter the string : ");
|
||||||
;
|
n = 0;
|
||||||
write(sockfd, buff, sizeof(buff));
|
while ((buff[n++] = getchar()) != '\n')
|
||||||
bzero(buff, sizeof(buff));
|
;
|
||||||
read(sockfd, buff, sizeof(buff));
|
write(sockfd, buff, sizeof(buff));
|
||||||
printf("From Server : %s", buff);
|
bzero(buff, sizeof(buff));
|
||||||
if ((strncmp(buff, "exit", 4)) == 0) {
|
read(sockfd, buff, sizeof(buff));
|
||||||
printf("Client Exit...\n");
|
printf("From Server : %s", buff);
|
||||||
break;
|
if ((strncmp(buff, "exit", 4)) == 0)
|
||||||
}
|
{
|
||||||
}
|
printf("Client Exit...\n");
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
int main()
|
}
|
||||||
{
|
}
|
||||||
int sockfd, connfd;
|
|
||||||
struct sockaddr_in servaddr, cli;
|
int main()
|
||||||
|
{
|
||||||
// socket create and varification
|
int sockfd, connfd;
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
struct sockaddr_in servaddr, cli;
|
||||||
if (sockfd == -1) {
|
|
||||||
printf("socket creation failed...\n");
|
// socket create and varification
|
||||||
exit(0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
}
|
if (sockfd == -1)
|
||||||
|
{
|
||||||
|
printf("socket creation failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
printf("Socket successfully created..\n");
|
printf("Socket successfully created..\n");
|
||||||
bzero(&servaddr, sizeof(servaddr));
|
bzero(&servaddr, sizeof(servaddr));
|
||||||
|
|
||||||
// assign IP, PORT
|
// assign IP, PORT
|
||||||
servaddr.sin_family = AF_INET;
|
servaddr.sin_family = AF_INET;
|
||||||
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||||
servaddr.sin_port = htons(PORT);
|
servaddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
// connect the client socket to server socket
|
// connect the client socket to server socket
|
||||||
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
|
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
|
||||||
printf("connection with the server failed...\n");
|
{
|
||||||
exit(0);
|
printf("connection with the server failed...\n");
|
||||||
}
|
exit(0);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
printf("connected to the server..\n");
|
printf("connected to the server..\n");
|
||||||
|
|
||||||
// function for chat
|
// function for chat
|
||||||
func(sockfd);
|
func(sockfd);
|
||||||
|
|
||||||
// close the socket
|
// close the socket
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
}
|
}
|
||||||
|
@ -1,100 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
void display(float a[20][20],int n)
|
|
||||||
{
|
|
||||||
int i,j;
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
for(j=0;j<=n;j++)
|
|
||||||
{
|
|
||||||
printf("%.2f \t",a[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
float interchange(float m[20][20],int i,int n)
|
|
||||||
{
|
|
||||||
float tmp[20][20];
|
|
||||||
float max=fabs(m[i][i]);
|
|
||||||
int j,k=i;
|
|
||||||
|
|
||||||
for(j=i;j<n;j++)
|
|
||||||
{
|
|
||||||
if(max<fabs(m[j][i]))
|
|
||||||
{
|
|
||||||
max=fabs(m[j][i]);
|
|
||||||
k=j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(j=0;j<=n;j++)
|
|
||||||
{
|
|
||||||
tmp[i][j]=m[i][j];
|
|
||||||
m[i][j]=m[k][j];
|
|
||||||
m[k][j]=tmp[i][j];
|
|
||||||
}
|
|
||||||
return m[20][20];
|
|
||||||
}
|
|
||||||
float eliminate(float m[20][20],int i,int n)
|
|
||||||
{
|
|
||||||
float tmp;
|
|
||||||
int k=1,l,j;
|
|
||||||
for(j=i;j<n-1;j++)
|
|
||||||
{
|
|
||||||
tmp=-((m[i+k][i])/(m[i][i]));
|
|
||||||
for(l=0;l<=n;l++)
|
|
||||||
{
|
|
||||||
m[i+k][l]=(m[i+k][l])+(m[i][l]*tmp);
|
|
||||||
}
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
return m[20][20];
|
|
||||||
}
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
int i,j,n,k=0,l;
|
|
||||||
float m[20][20],mul,tmp[20][20],val,ans[20];
|
|
||||||
clrscr();
|
|
||||||
|
|
||||||
printf("Total No.of Equations : ");
|
|
||||||
scanf("%d",&n);
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
|
|
||||||
printf("Enter Co-efficient Of Equations %d & Total --->>>\n",i+1);
|
|
||||||
for(j=0;j<=n;j++)
|
|
||||||
{
|
|
||||||
printf("r%d%d : ",i,j);
|
|
||||||
scanf("%f",&m[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
printf(":::::::::::: Current Matrix ::::::::::::\n\n");
|
|
||||||
display(m,n);
|
|
||||||
|
|
||||||
for(i=0;i<n-1;i++)
|
|
||||||
{
|
|
||||||
printf("\n------->>>>>>>>>>>>>>>>>>>>>>>>-------- %d\n",i+1);
|
|
||||||
m[20][20]=interchange(m,i,n);
|
|
||||||
display(m,n);
|
|
||||||
printf("\n_______________________________________\n");
|
|
||||||
m[20][20]=eliminate(m,i,n);
|
|
||||||
display(m,n);
|
|
||||||
}
|
|
||||||
printf("\n\n Values are : \n");
|
|
||||||
for(i=n-1;i>=0;i--)
|
|
||||||
{
|
|
||||||
l=n-1;
|
|
||||||
mul=0;
|
|
||||||
for(j=0;j<k;j++)
|
|
||||||
{
|
|
||||||
mul=mul+m[i][l]*ans[l];
|
|
||||||
l--;
|
|
||||||
}
|
|
||||||
k++;
|
|
||||||
ans[i]=(m[i][n]-mul)/m[i][i];
|
|
||||||
printf("X%d = %.2f\n",i+1,ans[i]);
|
|
||||||
}
|
|
||||||
getch();
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
#include<math.h>
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
int a[10],n,i,j,temp,sum=0;
|
|
||||||
float mean;
|
|
||||||
clrscr();
|
|
||||||
printf("Enter no. for Random Numbers :");
|
|
||||||
scanf("%d",&n);
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
a[i]=rand()%100;
|
|
||||||
}
|
|
||||||
printf("Random Numbers Generated are :\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
printf("\nSorted Data:");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
for(j=0;j<n;j++)
|
|
||||||
{
|
|
||||||
if(a[i]<a[j])
|
|
||||||
{
|
|
||||||
temp=a[i];
|
|
||||||
a[i]=a[j];
|
|
||||||
a[j]=temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
sum=sum+a[i];
|
|
||||||
}
|
|
||||||
mean=sum/(float)n;
|
|
||||||
printf("\nMean :");
|
|
||||||
printf("%f",mean);
|
|
||||||
|
|
||||||
getch();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,50 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
//#include<conio.h>
|
|
||||||
#include<math.h>
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
int a[10],n,i,j,temp;
|
|
||||||
float mean,median;
|
|
||||||
clrscr();
|
|
||||||
printf("Enter no. for Random Numbers :");
|
|
||||||
scanf("%d",&n);
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
a[i]=rand()%100;
|
|
||||||
}
|
|
||||||
printf("Random Numbers Generated are :\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
printf("\nSorted Data:");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
for(j=0;j<n;j++)
|
|
||||||
{
|
|
||||||
if(a[i]<a[j])
|
|
||||||
{
|
|
||||||
temp=a[i];
|
|
||||||
a[i]=a[j];
|
|
||||||
a[j]=temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("\n%d",a[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(n%2==0)
|
|
||||||
{
|
|
||||||
median=(a[n/2]+a[(n/2)-1])/2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
median=a[n/2];
|
|
||||||
}
|
|
||||||
printf("\nMedian is : %f",median);
|
|
||||||
getch();
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
#include<math.h>
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float a,b,c,a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,x1,x2,x3;
|
|
||||||
clrscr();
|
|
||||||
printf("Enter values of eq1:");
|
|
||||||
scanf("%f%f%f%f",&a1,&a2,&a3,&d1);
|
|
||||||
printf("Enter values of eq2:");
|
|
||||||
scanf("%f%f%f%f",&b1,&b2,&b3,&d2);
|
|
||||||
printf("Enter values of eq3:");
|
|
||||||
scanf("%f%f%f%f",&c1,&c2,&c3,&d3);
|
|
||||||
x1=x2=x3=0.0;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
a=x1;
|
|
||||||
b=x2;
|
|
||||||
c=x3;
|
|
||||||
x1=(1/a1)*(d1-(a2*x2)-(a3*x3));
|
|
||||||
x2=(1/b2)*(d2-(b1*x1)-(b3*x3));
|
|
||||||
x3=(1/c3)*(d3-(c1*x1)-(c2*x2));
|
|
||||||
}while(fabs(x1-a)>0.0001&&fabs(x2-b)>0.0001&&fabs(x3-c)>0.0001);
|
|
||||||
printf("x1=%f\nx2=%f\nx3=%f",x1,x2,x3);
|
|
||||||
getch();
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
#include<math.h>
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
float x[20],y[20],a,sum,p;
|
|
||||||
int n,i,j;
|
|
||||||
clrscr();
|
|
||||||
printf("Enter the no of entry to insert->");
|
|
||||||
scanf("%d",&n);
|
|
||||||
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("enter the value of x%d->",i);
|
|
||||||
scanf("%f",&x[i]);
|
|
||||||
printf("enter the value of y%d->",i);
|
|
||||||
scanf("%f",&y[i]);
|
|
||||||
}
|
|
||||||
printf("\n X \t\t Y \n");
|
|
||||||
printf("----------------------------\n");
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
printf("%f\t",x[i]);
|
|
||||||
printf("%f\n",y[i]);
|
|
||||||
}
|
|
||||||
printf("\nenter the value of x for interpolation:");
|
|
||||||
scanf("%f",&a)
|
|
||||||
sum=0;
|
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
p=1.0;
|
|
||||||
for(j=0;j<n;j++)
|
|
||||||
{
|
|
||||||
|
|
||||||
if(i !=j)
|
|
||||||
{
|
|
||||||
p=p*(a-x[j])/(x[i]-x[j]);
|
|
||||||
}
|
|
||||||
sum=sum+y[i]*p;
|
|
||||||
}
|
|
||||||
printf("ans is->%f",sum);
|
|
||||||
getch();
|
|
||||||
|
|
||||||
}}
|
|
@ -1,41 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<math.h>
|
|
||||||
|
|
||||||
float f(float x)
|
|
||||||
{
|
|
||||||
return 1.0+x*x*x; //This is the expresion of the function to integrate?
|
|
||||||
}
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
int i,n;
|
|
||||||
float a,b,h,x,s2,s3,sum,integral;
|
|
||||||
|
|
||||||
printf("enter the lower limit of the integration:");
|
|
||||||
scanf("%f",&a);
|
|
||||||
printf("enter the upper limit of the integration:");
|
|
||||||
scanf("%f",&b);
|
|
||||||
printf("enter the number of intervals:");
|
|
||||||
scanf("%d",&n);
|
|
||||||
|
|
||||||
h=(b-a)/n;
|
|
||||||
sum=f(a)+f(b);
|
|
||||||
s2=s3=0.0;
|
|
||||||
|
|
||||||
for(i=1;i<n;i+=3)
|
|
||||||
{
|
|
||||||
x=a+i*h;
|
|
||||||
s3=s3+f(x)+f(x+h);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i=3;i<n;i+=3)
|
|
||||||
{
|
|
||||||
x=a+i*h;
|
|
||||||
s2=s2+f(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
integral=(h/3.0)*(sum+2*s2+4*s3);
|
|
||||||
printf("\nValue of the integral = %9.4f\n",integral);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,3 +1,6 @@
|
|||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif(USE_OPENMP)
|
||||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
# automatically.
|
# automatically.
|
||||||
@ -7,10 +10,16 @@ file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
|||||||
foreach( testsourcefile ${APP_SOURCES} )
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
# I used a simple string replace, to cut off .cpp.
|
# I used a simple string replace, to cut off .cpp.
|
||||||
string( REPLACE ".c" "" testname ${testsourcefile} )
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
|
||||||
add_executable( ${testname} ${testsourcefile} )
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
# Make sure YourLib is linked to each app
|
# Make sure YourLib is linked to each app
|
||||||
target_link_libraries( ${testname} function_timer )
|
target_link_libraries( ${testname} function_timer )
|
||||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
|
|
||||||
install(TARGETS ${testname} DESTINATION "bin/conversions")
|
install(TARGETS ${testname} DESTINATION "bin/conversions")
|
||||||
|
|
||||||
endforeach( testsourcefile ${APP_SOURCES} )
|
endforeach( testsourcefile ${APP_SOURCES} )
|
||||||
|
@ -5,24 +5,25 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int decimal_to_octal(int decimal)
|
int decimal_to_octal(int decimal)
|
||||||
{
|
{
|
||||||
if( (decimal<8) && (decimal>0) )
|
if ((decimal < 8) && (decimal > 0))
|
||||||
{
|
{
|
||||||
return decimal;
|
return decimal;
|
||||||
}
|
}
|
||||||
else if(decimal==0)
|
else if (decimal == 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return ( (decimal_to_octal(decimal/8)*10) + decimal%8 );
|
return ((decimal_to_octal(decimal / 8) * 10) + decimal % 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void main()
|
int main()
|
||||||
{
|
{
|
||||||
int octalNumber,decimalNumber;
|
int octalNumber, decimalNumber;
|
||||||
printf("\nEnter your decimal number : ");
|
printf("\nEnter your decimal number : ");
|
||||||
scanf("%d",&decimalNumber);
|
scanf("%d", &decimalNumber);
|
||||||
octalNumber = decimal_to_octal(decimalNumber);
|
octalNumber = decimal_to_octal(decimalNumber);
|
||||||
printf("\nThe octal of %d is : %d" ,decimalNumber,octalNumber);
|
printf("\nThe octal of %d is : %d", decimalNumber, octalNumber);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,81 +4,82 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char hex[17];
|
#define MAX_STR_LEN 17
|
||||||
|
char hex[MAX_STR_LEN];
|
||||||
long long octal, bin, place;
|
long long octal, bin, place;
|
||||||
int i = 0, rem, val;
|
int i = 0, rem, val;
|
||||||
|
|
||||||
/* Input hexadecimal number from user */
|
/* Input hexadecimal number from user */
|
||||||
printf("Enter any hexadecimal number: ");
|
printf("Enter any hexadecimal number: ");
|
||||||
gets(hex);
|
fgets(hex, MAX_STR_LEN, stdin);
|
||||||
|
|
||||||
octal = 0ll;
|
octal = 0ll;
|
||||||
bin = 0ll;
|
bin = 0ll;
|
||||||
place = 0ll;
|
place = 0ll;
|
||||||
|
|
||||||
/* Hexadecimal to binary conversion */
|
/* Hexadecimal to binary conversion */
|
||||||
for(i=0; hex[i]!='\0'; i++)
|
for (i = 0; hex[i] != '\0'; i++)
|
||||||
{
|
{
|
||||||
bin = bin * place;
|
bin = bin * place;
|
||||||
|
|
||||||
switch(hex[i])
|
switch (hex[i])
|
||||||
{
|
{
|
||||||
case '0':
|
case '0':
|
||||||
bin += 0;
|
bin += 0;
|
||||||
break;
|
break;
|
||||||
case '1':
|
case '1':
|
||||||
bin += 1;
|
bin += 1;
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '2':
|
||||||
bin += 10;
|
bin += 10;
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
bin += 11;
|
bin += 11;
|
||||||
break;
|
break;
|
||||||
case '4':
|
case '4':
|
||||||
bin += 100;
|
bin += 100;
|
||||||
break;
|
break;
|
||||||
case '5':
|
case '5':
|
||||||
bin += 101;
|
bin += 101;
|
||||||
break;
|
break;
|
||||||
case '6':
|
case '6':
|
||||||
bin += 110;
|
bin += 110;
|
||||||
break;
|
break;
|
||||||
case '7':
|
case '7':
|
||||||
bin += 111;
|
bin += 111;
|
||||||
break;
|
break;
|
||||||
case '8':
|
case '8':
|
||||||
bin += 1000;
|
bin += 1000;
|
||||||
break;
|
break;
|
||||||
case '9':
|
case '9':
|
||||||
bin += 1001;
|
bin += 1001;
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
case 'A':
|
case 'A':
|
||||||
bin += 1010;
|
bin += 1010;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
case 'B':
|
case 'B':
|
||||||
bin += 1011;
|
bin += 1011;
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
case 'C':
|
case 'C':
|
||||||
bin += 1100;
|
bin += 1100;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
case 'D':
|
case 'D':
|
||||||
bin += 1101;
|
bin += 1101;
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
case 'E':
|
case 'E':
|
||||||
bin += 1110;
|
bin += 1110;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
case 'F':
|
case 'F':
|
||||||
bin += 1111;
|
bin += 1111;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Invalid hexadecimal input.");
|
printf("Invalid hexadecimal input.");
|
||||||
}
|
}
|
||||||
|
|
||||||
place = 10000;
|
place = 10000;
|
||||||
@ -87,36 +88,36 @@ int main()
|
|||||||
place = 1;
|
place = 1;
|
||||||
|
|
||||||
/* Binary to octal conversion */
|
/* Binary to octal conversion */
|
||||||
while(bin > 0)
|
while (bin > 0)
|
||||||
{
|
{
|
||||||
rem = bin % 1000;
|
rem = bin % 1000;
|
||||||
|
|
||||||
switch(rem)
|
switch (rem)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
val = 0;
|
val = 0;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
val = 1;
|
val = 1;
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
val = 2;
|
val = 2;
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
val = 3;
|
val = 3;
|
||||||
break;
|
break;
|
||||||
case 100:
|
case 100:
|
||||||
val = 4;
|
val = 4;
|
||||||
break;
|
break;
|
||||||
case 101:
|
case 101:
|
||||||
val = 5;
|
val = 5;
|
||||||
break;
|
break;
|
||||||
case 110:
|
case 110:
|
||||||
val = 6;
|
val = 6;
|
||||||
break;
|
break;
|
||||||
case 111:
|
case 111:
|
||||||
val = 7;
|
val = 7;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
octal = (val * place) + octal;
|
octal = (val * place) + octal;
|
||||||
@ -129,4 +130,4 @@ int main()
|
|||||||
printf("Octal number = %lld", octal);
|
printf("Octal number = %lld", octal);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 327ddab3e895c26026eeb39ed7e0b44d82597137
|
Subproject commit e0dc782b7b0f162299d10d94b0132f111d89c22f
|
@ -1,20 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
author: Christian Bender
|
author: Christian Bender
|
||||||
This file contains a simple test program for each hash-function.
|
This file contains a simple test program for each hash-function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char s[] = "hello";
|
char s[] = "hello";
|
||||||
|
|
||||||
/* actual tests */
|
/* actual tests */
|
||||||
printf("sdbm: %s --> %llX\n", s, sdbm(s));
|
printf("sdbm: %s --> %llX\n", s, sdbm(s));
|
||||||
printf("djb2: %s --> %llX\n", s, djb2(s));
|
printf("djb2: %s --> %llX\n", s, djb2(s));
|
||||||
printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */
|
printf("xor8: %s --> %X\n", s, xor8(s)); /* 8 bit */
|
||||||
printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */
|
printf("adler_32: %s --> %X\n", s, adler_32(s)); /* 32 bit */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
|
||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif()
|
||||||
|
|
||||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
# automatically.
|
# automatically.
|
||||||
@ -11,6 +16,9 @@ foreach( testsourcefile ${APP_SOURCES} )
|
|||||||
# Make sure YourLib is linked to each app
|
# Make sure YourLib is linked to each app
|
||||||
target_link_libraries( ${testname} function_timer )
|
target_link_libraries( ${testname} function_timer )
|
||||||
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
install(TARGETS ${testname} DESTINATION "bin/misc")
|
install(TARGETS ${testname} DESTINATION "bin/misc")
|
||||||
|
|
||||||
endforeach( testsourcefile ${APP_SOURCES} )
|
endforeach( testsourcefile ${APP_SOURCES} )
|
||||||
|
101
misc/rselect.c
101
misc/rselect.c
@ -1,54 +1,77 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include<stdlib.h>
|
#include <stdlib.h>
|
||||||
#include<time.h>
|
#include <time.h>
|
||||||
void swap(int *a ,int *b)
|
void swap(int *a, int *b)
|
||||||
{int t;t =*a;*a=*b;*b=t;}
|
{
|
||||||
int part(int a[],int l,int r,int n,int pivot,int pindex)
|
int t;
|
||||||
{int p1=l,p2=r;
|
t = *a;
|
||||||
while(p2>p1)
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
}
|
||||||
|
int part(int a[], int l, int r, int n, int pivot, int pindex)
|
||||||
|
{
|
||||||
|
int p1 = l, p2 = r;
|
||||||
|
while (p2 > p1)
|
||||||
{
|
{
|
||||||
if (a[p1] > pivot && a[p2]<pivot)
|
if (a[p1] > pivot && a[p2] < pivot)
|
||||||
{swap(&a[p1],&a[p2]);}
|
{
|
||||||
|
swap(&a[p1], &a[p2]);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (a[p1] <=pivot)
|
if (a[p1] <= pivot)
|
||||||
{p1++;}
|
{
|
||||||
if (a[p2]>=pivot)
|
p1++;
|
||||||
{p2--;}
|
}
|
||||||
|
if (a[p2] >= pivot)
|
||||||
|
{
|
||||||
|
p2--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
swap(&a[pindex],&a[p2]);
|
swap(&a[pindex], &a[p2]);
|
||||||
return p2;
|
return p2;
|
||||||
}
|
}
|
||||||
int rselect(int a[],int l,int r,int n,int o)
|
int rselect(int a[], int l, int r, int n, int o)
|
||||||
{
|
{
|
||||||
int pivot,pindex,pactual;
|
int pivot, pindex, pactual;
|
||||||
if (r>l)
|
if (r > l)
|
||||||
{
|
{
|
||||||
pindex = rand()%(r-l+1);
|
pindex = rand() % (r - l + 1);
|
||||||
pivot = a[pindex];
|
pivot = a[pindex];
|
||||||
pactual = part(a,l,r,n,pivot,pindex);
|
pactual = part(a, l, r, n, pivot, pindex);
|
||||||
|
|
||||||
if (pactual == o)
|
if (pactual == o)
|
||||||
{return a[pactual];}
|
{
|
||||||
|
return a[pactual];
|
||||||
if (o < pactual)
|
}
|
||||||
{rselect(a,l,pactual-1,n,o);}
|
|
||||||
|
if (o < pactual)
|
||||||
if (o>pactual)
|
{
|
||||||
{rselect(a,pactual+1,r,n,o-pactual);}
|
rselect(a, l, pactual - 1, n, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o > pactual)
|
||||||
|
{
|
||||||
|
rselect(a, pactual + 1, r, n, o - pactual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (r==l)
|
if (r == l)
|
||||||
{return a[l];}
|
{
|
||||||
|
return a[l];
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
int main()
|
int main()
|
||||||
{srand(time(NULL));
|
{
|
||||||
int n,o,i,*a;
|
srand(time(NULL));
|
||||||
scanf("%d %d",&n,&o);
|
int n, o, i, *a;
|
||||||
a = (int*)malloc(n*sizeof(int));
|
scanf("%d %d", &n, &o);
|
||||||
for (i=0;i<n;i++)
|
a = (int *)malloc(n * sizeof(int));
|
||||||
{scanf("%d",a+i);}
|
for (i = 0; i < n; i++)
|
||||||
printf("\n\n%d",rselect(a,0,n-1,n,o));
|
{
|
||||||
|
scanf("%d", a + i);
|
||||||
|
}
|
||||||
|
printf("\n\n%d", rselect(a, 0, n - 1, n, o));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,38 +1,38 @@
|
|||||||
/**
|
/**
|
||||||
* Modified on 07/12/2017, Kyler Smith
|
* Modified on 07/12/2017, Kyler Smith
|
||||||
*
|
*
|
||||||
* A number is called strong number if sum of the
|
* A number is called strong number if sum of the
|
||||||
* factorial of its digit is equal to number itself.
|
* factorial of its digit is equal to number itself.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
void strng(int a)
|
void strng(int a)
|
||||||
{
|
{
|
||||||
int j=a;
|
int j = a;
|
||||||
int sum=0;
|
int sum = 0;
|
||||||
int b,i,fact=1;
|
int b, i, fact = 1;
|
||||||
while(a>0)
|
while (a > 0)
|
||||||
{
|
{
|
||||||
fact=1;
|
fact = 1;
|
||||||
b=a%10;
|
b = a % 10;
|
||||||
for(i=1;i<=b;i++)
|
for (i = 1; i <= b; i++)
|
||||||
{
|
{
|
||||||
fact=fact*i;
|
fact = fact * i;
|
||||||
}
|
}
|
||||||
a=a/10;
|
a = a / 10;
|
||||||
sum=sum+fact;
|
sum = sum + fact;
|
||||||
}
|
}
|
||||||
if(sum==j)
|
if (sum == j)
|
||||||
printf("%d is a strong number",j);
|
printf("%d is a strong number", j);
|
||||||
else
|
else
|
||||||
printf("%d is not a strong number",j);
|
printf("%d is not a strong number", j);
|
||||||
}
|
}
|
||||||
void main()
|
int main()
|
||||||
{
|
{
|
||||||
int a;
|
int a;
|
||||||
printf("Enter the number to check");
|
printf("Enter the number to check");
|
||||||
scanf("%d",&a);
|
scanf("%d", &a);
|
||||||
strng(a);
|
strng(a);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
26
numerical_methods/CMakeLists.txt
Normal file
26
numerical_methods/CMakeLists.txt
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif(USE_OPENMP)
|
||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
string( REPLACE " " "_" testname ${testsourcefile} )
|
||||||
|
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
|
# Make sure YourLib is linked to each app
|
||||||
|
target_link_libraries( ${testname} function_timer )
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/stats")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
104
numerical_methods/Gauss_Elimination.c
Normal file
104
numerical_methods/Gauss_Elimination.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define ARRAY_SIZE 20
|
||||||
|
|
||||||
|
void display(float a[ARRAY_SIZE][ARRAY_SIZE], int n)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j <= n; j++)
|
||||||
|
{
|
||||||
|
printf("%.2f \t", a[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float interchange(float m[ARRAY_SIZE][ARRAY_SIZE], int i, int n)
|
||||||
|
{
|
||||||
|
float tmp[ARRAY_SIZE][ARRAY_SIZE];
|
||||||
|
float max = fabs(m[i][i]);
|
||||||
|
int j, k = i;
|
||||||
|
|
||||||
|
for (j = i; j < n; j++)
|
||||||
|
{
|
||||||
|
if (max < fabs(m[j][i]))
|
||||||
|
{
|
||||||
|
max = fabs(m[j][i]);
|
||||||
|
k = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (j = 0; j <= n; j++)
|
||||||
|
{
|
||||||
|
tmp[i][j] = m[i][j];
|
||||||
|
m[i][j] = m[k][j];
|
||||||
|
m[k][j] = tmp[i][j];
|
||||||
|
}
|
||||||
|
return m[ARRAY_SIZE - 1][ARRAY_SIZE - 1];
|
||||||
|
}
|
||||||
|
float eliminate(float m[ARRAY_SIZE][ARRAY_SIZE], int i, int n)
|
||||||
|
{
|
||||||
|
float tmp;
|
||||||
|
int k = 1, l, j;
|
||||||
|
for (j = i; j < n - 1; j++)
|
||||||
|
{
|
||||||
|
tmp = -((m[i + k][i]) / (m[i][i]));
|
||||||
|
for (l = 0; l <= n; l++)
|
||||||
|
{
|
||||||
|
m[i + k][l] = (m[i + k][l]) + (m[i][l] * tmp);
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
return m[ARRAY_SIZE - 1][ARRAY_SIZE - 1];
|
||||||
|
}
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i, j, n, k = 0, l;
|
||||||
|
float m[ARRAY_SIZE][ARRAY_SIZE], mul, tmp[ARRAY_SIZE][ARRAY_SIZE], val, ans[ARRAY_SIZE];
|
||||||
|
|
||||||
|
printf("Total No.of Equations : ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("Enter Co-efficient Of Equations %d & Total --->>>\n", i + 1);
|
||||||
|
for (j = 0; j <= n; j++)
|
||||||
|
{
|
||||||
|
printf("r%d%d : ", i, j);
|
||||||
|
scanf("%f", &m[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf(":::::::::::: Current Matrix ::::::::::::\n\n");
|
||||||
|
display(m, n);
|
||||||
|
|
||||||
|
for (i = 0; i < n - 1; i++)
|
||||||
|
{
|
||||||
|
printf("\n------->>>>>>>>>>>>>>>>>>>>>>>>-------- %d\n", i + 1);
|
||||||
|
m[ARRAY_SIZE - 1][ARRAY_SIZE - 1] = interchange(m, i, n);
|
||||||
|
display(m, n);
|
||||||
|
printf("\n_______________________________________\n");
|
||||||
|
m[ARRAY_SIZE - 1][ARRAY_SIZE - 1] = eliminate(m, i, n);
|
||||||
|
display(m, n);
|
||||||
|
}
|
||||||
|
printf("\n\n Values are : \n");
|
||||||
|
for (i = n - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
l = n - 1;
|
||||||
|
mul = 0;
|
||||||
|
for (j = 0; j < k; j++)
|
||||||
|
{
|
||||||
|
mul = mul + m[i][l] * ans[l];
|
||||||
|
l--;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
ans[i] = (m[i][n] - mul) / m[i][i];
|
||||||
|
printf("X%d = %.2f\n", i + 1, ans[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
39
numerical_methods/MEAN.C
Normal file
39
numerical_methods/MEAN.C
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#define MAX_LEN INT_MAX
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int a[MAX_LEN], n = 10, i, j, temp, sum = 0;
|
||||||
|
float mean;
|
||||||
|
|
||||||
|
if (argc == 2)
|
||||||
|
{
|
||||||
|
n = atoi(argv[1]);
|
||||||
|
if (n >= MAX_LEN)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Maximum %d!\n", MAX_LEN);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Random Numbers Generated are : ");
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
a[i] = rand() % 100;
|
||||||
|
printf("%2d, ", a[i]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
sum = sum + a[i];
|
||||||
|
|
||||||
|
mean = sum / (float)n;
|
||||||
|
printf("\nMean :");
|
||||||
|
printf("%f", mean);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
51
numerical_methods/MEDIAN.C
Normal file
51
numerical_methods/MEDIAN.C
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a[10], n, i, j, temp;
|
||||||
|
float mean, median;
|
||||||
|
|
||||||
|
printf("Enter no. for Random Numbers :");
|
||||||
|
scanf("%d", &n);
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
a[i] = rand() % 100;
|
||||||
|
}
|
||||||
|
printf("Random Numbers Generated are :\n");
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("\n%d", a[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
printf("\nSorted Data:");
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
{
|
||||||
|
if (a[i] < a[j])
|
||||||
|
{
|
||||||
|
temp = a[i];
|
||||||
|
a[i] = a[j];
|
||||||
|
a[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("\n%d", a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n % 2 == 0)
|
||||||
|
{
|
||||||
|
median = (a[n / 2] + a[(n / 2) - 1]) / 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
median = a[n / 2];
|
||||||
|
}
|
||||||
|
printf("\nMedian is : %f", median);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
27
numerical_methods/Seidal.C
Normal file
27
numerical_methods/Seidal.C
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float a, b, c, a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, x1, x2, x3;
|
||||||
|
|
||||||
|
printf("Enter values of eq1:");
|
||||||
|
scanf("%f%f%f%f", &a1, &a2, &a3, &d1);
|
||||||
|
printf("Enter values of eq2:");
|
||||||
|
scanf("%f%f%f%f", &b1, &b2, &b3, &d2);
|
||||||
|
printf("Enter values of eq3:");
|
||||||
|
scanf("%f%f%f%f", &c1, &c2, &c3, &d3);
|
||||||
|
x1 = x2 = x3 = 0.0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
a = x1;
|
||||||
|
b = x2;
|
||||||
|
c = x3;
|
||||||
|
x1 = (1 / a1) * (d1 - (a2 * x2) - (a3 * x3));
|
||||||
|
x2 = (1 / b2) * (d2 - (b1 * x1) - (b3 * x3));
|
||||||
|
x3 = (1 / c3) * (d3 - (c1 * x1) - (c2 * x2));
|
||||||
|
} while (fabs(x1 - a) > 0.0001 && fabs(x2 - b) > 0.0001 && fabs(x3 - c) > 0.0001);
|
||||||
|
printf("x1=%f\nx2=%f\nx3=%f", x1, x2, x3);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
46
numerical_methods/lagrange_theorem.C
Normal file
46
numerical_methods/lagrange_theorem.C
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float x[20], y[20], a, sum, p;
|
||||||
|
int n, i, j;
|
||||||
|
|
||||||
|
printf("Enter the no of entry to insert->");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("enter the value of x%d->", i);
|
||||||
|
scanf("%f", &x[i]);
|
||||||
|
printf("enter the value of y%d->", i);
|
||||||
|
scanf("%f", &y[i]);
|
||||||
|
}
|
||||||
|
printf("\n X \t\t Y \n");
|
||||||
|
printf("----------------------------\n");
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("%f\t", x[i]);
|
||||||
|
printf("%f\n", y[i]);
|
||||||
|
}
|
||||||
|
printf("\nenter the value of x for interpolation:");
|
||||||
|
scanf("%f", &a);
|
||||||
|
sum = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
p = 1.0;
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (i != j)
|
||||||
|
{
|
||||||
|
p = p * (a - x[j]) / (x[i] - x[j]);
|
||||||
|
}
|
||||||
|
sum = sum + y[i] * p;
|
||||||
|
}
|
||||||
|
printf("ans is->%f", sum);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
68
numerical_methods/newton-raphson-root.c
Normal file
68
numerical_methods/newton-raphson-root.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/***
|
||||||
|
* approximate solution for f(x) = 0
|
||||||
|
* given f(x) and f'(x)
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <complex.h> /* requires minimum of C99 */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* f(x)
|
||||||
|
*/
|
||||||
|
double complex function(double complex x)
|
||||||
|
{
|
||||||
|
return x * x - 3.; /* x^2 = 3 - solution is sqrt(3) */
|
||||||
|
// return x * x - 2.; /* x^2 = 2 - solution is sqrt(2) */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* f'(x)
|
||||||
|
*/
|
||||||
|
double complex d_function(double complex x)
|
||||||
|
{
|
||||||
|
return 2. * x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const double accuracy = 1e-10;
|
||||||
|
double delta = 1;
|
||||||
|
double complex cdelta = 1;
|
||||||
|
|
||||||
|
/* initialize random seed: */
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
double complex root = (rand() % 100 - 50) + (random() % 100 - 50) * I;
|
||||||
|
|
||||||
|
unsigned long counter = 0;
|
||||||
|
while (delta > accuracy && counter < ULONG_MAX)
|
||||||
|
{
|
||||||
|
cdelta = function(root) / d_function(root);
|
||||||
|
root += -cdelta;
|
||||||
|
counter++;
|
||||||
|
delta = fabs(cabs(cdelta));
|
||||||
|
|
||||||
|
#if defined(DEBUG) || !defined(NDEBUG)
|
||||||
|
if (counter % 50 == 0)
|
||||||
|
{
|
||||||
|
double r = creal(root);
|
||||||
|
double c = cimag(root);
|
||||||
|
|
||||||
|
printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r,
|
||||||
|
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
double r = creal(root);
|
||||||
|
double c = fabs(cimag(root)) < accuracy ? 0 : cimag(root);
|
||||||
|
|
||||||
|
printf("Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r,
|
||||||
|
c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
41
numerical_methods/simpsons_1-3rd rule.c
Normal file
41
numerical_methods/simpsons_1-3rd rule.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float f(float x)
|
||||||
|
{
|
||||||
|
return 1.0 + x * x * x; //This is the expresion of the function to integrate?
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i, n;
|
||||||
|
float a, b, h, x, s2, s3, sum, integral;
|
||||||
|
|
||||||
|
printf("enter the lower limit of the integration:");
|
||||||
|
scanf("%f", &a);
|
||||||
|
printf("enter the upper limit of the integration:");
|
||||||
|
scanf("%f", &b);
|
||||||
|
printf("enter the number of intervals:");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
h = (b - a) / n;
|
||||||
|
sum = f(a) + f(b);
|
||||||
|
s2 = s3 = 0.0;
|
||||||
|
|
||||||
|
for (i = 1; i < n; i += 3)
|
||||||
|
{
|
||||||
|
x = a + i * h;
|
||||||
|
s3 = s3 + f(x) + f(x + h);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 3; i < n; i += 3)
|
||||||
|
{
|
||||||
|
x = a + i * h;
|
||||||
|
s2 = s2 + f(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
integral = (h / 3.0) * (sum + 2 * s2 + 4 * s3);
|
||||||
|
printf("\nValue of the integral = %9.4f\n", integral);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -6,47 +6,61 @@ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int isprime(int no) {
|
int isprime(int no)
|
||||||
|
{
|
||||||
int sq;
|
int sq;
|
||||||
|
|
||||||
if (no == 2) {
|
if (no == 2)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else if (no%2 == 0) {
|
else if (no % 2 == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
sq = ((int)(sqrt(no))) + 1;
|
sq = ((int)(sqrt(no))) + 1;
|
||||||
for (int i = 3; i < sq; i + 2) {
|
for (int i = 3; i < sq; i += 2)
|
||||||
if (no%i == 0) {
|
{
|
||||||
|
if (no % i == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
int maxNumber = 0;
|
int maxNumber = 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int n1;
|
int n1;
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
if (isprime(n) == 1)
|
if (isprime(n) == 1)
|
||||||
printf("%d", n);
|
printf("%d", n);
|
||||||
else {
|
else
|
||||||
while (n % 2 == 0) {
|
{
|
||||||
|
while (n % 2 == 0)
|
||||||
|
{
|
||||||
n = n / 2;
|
n = n / 2;
|
||||||
}
|
}
|
||||||
if (isprime(n) == 1) {
|
if (isprime(n) == 1)
|
||||||
|
{
|
||||||
printf("%d\n", n);
|
printf("%d\n", n);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
n1 = ((int)(sqrt(n))) + 1;
|
n1 = ((int)(sqrt(n))) + 1;
|
||||||
for (int i = 3; i < n1; i + 2) {
|
for (int i = 3; i < n1; i += 2)
|
||||||
if (n%i == 0) {
|
{
|
||||||
if (isprime((int)(n / i)) == 1) {
|
if (n % i == 0)
|
||||||
|
{
|
||||||
|
if (isprime((int)(n / i)) == 1)
|
||||||
|
{
|
||||||
maxNumber = n / i;
|
maxNumber = n / i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (isprime(i) == 1) {
|
else if (isprime(i) == 1)
|
||||||
|
{
|
||||||
maxNumber = i;
|
maxNumber = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,34 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
unsigned long gcd(unsigned long a, unsigned long b) {
|
unsigned long gcd(unsigned long a, unsigned long b)
|
||||||
|
{
|
||||||
unsigned long r;
|
unsigned long r;
|
||||||
if (a > b) {
|
if (a > b)
|
||||||
|
{
|
||||||
unsigned long t = a;
|
unsigned long t = a;
|
||||||
a = b;
|
a = b;
|
||||||
b = t;
|
b = t;
|
||||||
}
|
}
|
||||||
while (r = a % b) {
|
while ((r = (a % b)))
|
||||||
|
{
|
||||||
a = b;
|
a = b;
|
||||||
b = r;
|
b = r;
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lcm(unsigned long a, unsigned long b) {
|
unsigned long lcm(unsigned long a, unsigned long b)
|
||||||
|
{
|
||||||
unsigned long long p = (unsigned long long)a * b;
|
unsigned long long p = (unsigned long long)a * b;
|
||||||
return p / gcd(a, b);
|
return p / gcd(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void)
|
||||||
|
{
|
||||||
unsigned long ans = 1;
|
unsigned long ans = 1;
|
||||||
unsigned long i;
|
unsigned long i;
|
||||||
for (i = 1; i <= 20; i++) {
|
for (i = 1; i <= 20; i++)
|
||||||
|
{
|
||||||
ans = lcm(ans, i);
|
ans = lcm(ans, i);
|
||||||
}
|
}
|
||||||
printf("%lu\n", ans);
|
printf("%lu\n", ans);
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int64_t get_product(FILE *fp, long start_pos, int num_digits)
|
long long int get_product(FILE *fp, long start_pos, int num_digits)
|
||||||
{
|
{
|
||||||
char ch = ' '; /* temporary variable to store character read from file */
|
char ch = ' '; /* temporary variable to store character read from file */
|
||||||
uint8_t num = 0; /* temporary variable to store digit read */
|
unsigned char num = 0; /* temporary variable to store digit read */
|
||||||
int64_t prod = 1; /* product accumulator */
|
long long int prod = 1; /* product accumulator */
|
||||||
int count = 0; /* we use this variable to count number of bytes of file read */
|
int count = 0; /* we use this variable to count number of bytes of file read */
|
||||||
|
|
||||||
/* accumulate product for num_digits */
|
/* accumulate product for num_digits */
|
||||||
for (int i = 0; i < num_digits; i++, count++)
|
for (int i = 0; i < num_digits; i++, count++)
|
||||||
@ -49,7 +48,7 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
int position = 0;
|
int position = 0;
|
||||||
int num_digits = 4;
|
int num_digits = 4;
|
||||||
int64_t prod, max_prod = 0;
|
long long int prod, max_prod = 0;
|
||||||
|
|
||||||
/* if second command-line argument is ge=iven,
|
/* if second command-line argument is ge=iven,
|
||||||
* use it as the number of digits to compute
|
* use it as the number of digits to compute
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h> /* for memmove */
|
#include <string.h> /* for memmove */
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@ -8,9 +7,9 @@ int main(int argc, char *argv[])
|
|||||||
int position = 0, num_bad_chars = 0;
|
int position = 0, num_bad_chars = 0;
|
||||||
int num_digits = 4;
|
int num_digits = 4;
|
||||||
char ch;
|
char ch;
|
||||||
uint8_t num, num_prev;
|
unsigned char num, num_prev;
|
||||||
uint8_t *buffer = NULL;
|
unsigned char *buffer = NULL;
|
||||||
int64_t prod = 1, max_prod = 0;
|
long long int prod = 1, max_prod = 0;
|
||||||
|
|
||||||
/* if second command-line argument is given,
|
/* if second command-line argument is given,
|
||||||
* use it as the number of digits to compute
|
* use it as the number of digits to compute
|
||||||
@ -20,7 +19,7 @@ int main(int argc, char *argv[])
|
|||||||
num_digits = atoi(argv[1]);
|
num_digits = atoi(argv[1]);
|
||||||
|
|
||||||
/* allocate memory to store past values */
|
/* allocate memory to store past values */
|
||||||
buffer = calloc(num_digits, sizeof(uint8_t));
|
buffer = calloc(num_digits, sizeof(unsigned char));
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
perror("Unable to allocate memory for buffer");
|
perror("Unable to allocate memory for buffer");
|
||||||
|
@ -2,27 +2,24 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#pragma message ("Using OpenMP parallelization")
|
|
||||||
#else
|
|
||||||
#pragma message ("Not using OpenMP parallelization")
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the length of collatz sequence for a given
|
* Computes the length of collatz sequence for a given
|
||||||
* starting number
|
* starting number
|
||||||
**/
|
**/
|
||||||
long long collatz(long long start_num)
|
long long collatz(long long start_num)
|
||||||
{
|
{
|
||||||
long long length = 1;
|
long long length = 1;
|
||||||
|
|
||||||
while (start_num != 1) /* loop till we reach 1 */
|
while (start_num != 1) /* loop till we reach 1 */
|
||||||
{
|
{
|
||||||
if(start_num & 0x01) /* check for odd */
|
if (start_num & 0x01) /* check for odd */
|
||||||
start_num = 3 * start_num + 1;
|
start_num = 3 * start_num + 1;
|
||||||
else
|
else
|
||||||
start_num >>= 1; /* simpler divide by 2 */
|
start_num >>= 1; /* simpler divide by 2 */
|
||||||
length ++;
|
length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
@ -33,39 +30,39 @@ int main(int argc, char **argv)
|
|||||||
long long max_len = 0, max_len_num = 0;
|
long long max_len = 0, max_len_num = 0;
|
||||||
long long MAX_NUM = 1000000;
|
long long MAX_NUM = 1000000;
|
||||||
|
|
||||||
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
|
if (argc == 2) /* set commandline argumnet as the maximum iteration number */
|
||||||
{
|
{
|
||||||
MAX_NUM = atoll(argv[1]);
|
MAX_NUM = atoll(argv[1]);
|
||||||
printf("Maximum number: %lld\n", MAX_NUM);
|
printf("Maximum number: %lld\n", MAX_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Since the computational values for each iteration step are independent,
|
* Since the computational values for each iteration step are independent,
|
||||||
* we can compute them in parallel. However, the maximum values should be
|
* we can compute them in parallel. However, the maximum values should be
|
||||||
* updated in synchrony so that we do not get into a "race condition".
|
* updated in synchrony so that we do not get into a "race condition".
|
||||||
*
|
*
|
||||||
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be passes
|
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be passes
|
||||||
* while with Microsoft C compiler, the flag "/fopenmp" should be used.
|
* while with Microsoft C compiler, the flag "/fopenmp" should be used.
|
||||||
*
|
*
|
||||||
* Automatically detects for OPENMP using the _OPENMP macro.
|
* Automatically detects for OPENMP using the _OPENMP macro.
|
||||||
**/
|
**/
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
|
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
|
||||||
#endif
|
#endif
|
||||||
for (long long i = 1; i < MAX_NUM; i++)
|
for (long long i = 1; i < MAX_NUM; i++)
|
||||||
{
|
{
|
||||||
long long L = collatz(i);
|
long long L = collatz(i);
|
||||||
if (L > max_len)
|
if (L > max_len)
|
||||||
{
|
{
|
||||||
max_len = L; /* length of sequence */
|
max_len = L; /* length of sequence */
|
||||||
max_len_num = i; /* starting number */
|
max_len_num = i; /* starting number */
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_OPENMP) && defined(DEBUG)
|
#if defined(_OPENMP) && defined(DEBUG)
|
||||||
printf("Thread: %2d\t %3lld: \t%5lld\n", omp_get_thread_num(), i, L);
|
printf("Thread: %2d\t %3lld: \t%5lld\n", omp_get_thread_num(), i, L);
|
||||||
#elif defined(DEBUG)
|
#elif defined(DEBUG)
|
||||||
printf("%3lld: \t%5lld\n", i, L);
|
printf("%3lld: \t%5lld\n", i, L);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Start: %3lld: \tLength: %5lld\n", max_len_num, max_len);
|
printf("Start: %3lld: \tLength: %5lld\n", max_len_num, max_len);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
74
project_euler/Problem 26/sol1.c
Normal file
74
project_euler/Problem 26/sol1.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#include <omp.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_DENO 2000
|
||||||
|
#define MAX_LEN (MAX_DENO + 10)
|
||||||
|
|
||||||
|
int compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return (*(unsigned short *)a - *(unsigned short *)b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
unsigned short max_digits = 0, max_idx_number = 0;
|
||||||
|
|
||||||
|
clock_t start_time = clock();
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (unsigned short deno = 2; deno < MAX_DENO; deno++)
|
||||||
|
{
|
||||||
|
unsigned short remainders[MAX_LEN];
|
||||||
|
unsigned short rem = 1, *rem_ptr = remainders;
|
||||||
|
memset(remainders, (unsigned short)-1, MAX_LEN * sizeof(unsigned short));
|
||||||
|
// remainders[0] = 1;
|
||||||
|
// printf("1/%-4u\t ", deno);
|
||||||
|
unsigned short index = 0, num_digits;
|
||||||
|
|
||||||
|
while (rem != 0)
|
||||||
|
{
|
||||||
|
rem = (rem * 10) % deno;
|
||||||
|
if (rem == 0)
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rem_ptr = (unsigned short *)bsearch(&rem, remainders, MAX_LEN, sizeof(unsigned short), compare);
|
||||||
|
// printf("%2d, ", rem);
|
||||||
|
// printf("(%14p), ", rem_ptr);
|
||||||
|
if (rem_ptr != NULL)
|
||||||
|
break;
|
||||||
|
remainders[index] = rem;
|
||||||
|
rem_ptr = remainders;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
num_digits = index - (rem_ptr - remainders);
|
||||||
|
// printf("\n\t(%14p, %14p, %4u, %4u)\n", rem_ptr, remainders, index, num_digits);
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#pragma omp critical
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
if (num_digits > max_digits)
|
||||||
|
{
|
||||||
|
max_digits = num_digits;
|
||||||
|
max_idx_number = deno;
|
||||||
|
// printf("\t (%u, %u)\n ", max_digits, max_idx_number);
|
||||||
|
}
|
||||||
|
#ifdef _OPENMP
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
clock_t end_time = clock();
|
||||||
|
|
||||||
|
printf("Time taken: %.4g ms\n", 1e3 * (double)(end_time - start_time) / CLOCKS_PER_SEC);
|
||||||
|
printf("Maximum digits: %hu\t Denominator: %hu\n", max_digits, max_idx_number);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
23
searching/CMakeLists.txt
Normal file
23
searching/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
# Make sure YourLib is linked to each app
|
||||||
|
target_link_libraries( ${testname} function_timer )
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/searching")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -1,27 +1,32 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int linearsearch(int *arr, int size, int val){
|
int linearsearch(int *arr, int size, int val)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < size; i++){
|
for (i = 0; i < size; i++)
|
||||||
|
{
|
||||||
if (arr[i] == val)
|
if (arr[i] == val)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void main(){
|
int main()
|
||||||
int n,i,v;
|
{
|
||||||
|
int n, i, v;
|
||||||
printf("Enter the size of the array:\n");
|
printf("Enter the size of the array:\n");
|
||||||
scanf("%d",&n); //Taking input for the size of Array
|
scanf("%d", &n); //Taking input for the size of Array
|
||||||
|
|
||||||
int a[n];
|
int a[n];
|
||||||
printf("Enter the contents for an array of size %d:\n", n);
|
printf("Enter the contents for an array of size %d:\n", n);
|
||||||
for (i = 0; i < n; i++) scanf("%d", &a[i]);// accepts the values of array elements until the loop terminates//
|
for (i = 0; i < n; i++)
|
||||||
|
scanf("%d", &a[i]); // accepts the values of array elements until the loop terminates//
|
||||||
|
|
||||||
printf("Enter the value to be searched:\n");
|
printf("Enter the value to be searched:\n");
|
||||||
scanf("%d", &v); //Taking input the value to be searched
|
scanf("%d", &v); //Taking input the value to be searched
|
||||||
if (linearsearch(a,n,v))
|
if (linearsearch(a, n, v))
|
||||||
printf("Value %d is in the array.\n", v);
|
printf("Value %d is in the array.\n", v);
|
||||||
else
|
else
|
||||||
printf("Value %d is not in the array.\n", v);
|
printf("Value %d is not in the array.\n", v);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#define len 5
|
#define len 5
|
||||||
|
|
||||||
|
|
||||||
int binarySearch(int array[], int leng, int searchX)
|
int binarySearch(int array[], int leng, int searchX)
|
||||||
{
|
{
|
||||||
int pos = -1, right, left, i = 0;
|
int pos = -1, right, left, i = 0;
|
||||||
@ -10,14 +9,14 @@ int binarySearch(int array[], int leng, int searchX)
|
|||||||
left = 0;
|
left = 0;
|
||||||
right = leng - 1;
|
right = leng - 1;
|
||||||
|
|
||||||
while(left <= right)
|
while (left <= right)
|
||||||
{
|
{
|
||||||
pos = left + (right - left) / 2;
|
pos = left + (right - left) / 2;
|
||||||
if(array[pos] == searchX)
|
if (array[pos] == searchX)
|
||||||
{
|
{
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
else if(array[pos] > searchX)
|
else if (array[pos] > searchX)
|
||||||
{
|
{
|
||||||
right = pos - 1;
|
right = pos - 1;
|
||||||
}
|
}
|
||||||
@ -29,10 +28,9 @@ int binarySearch(int array[], int leng, int searchX)
|
|||||||
return -1; /* not found */
|
return -1; /* not found */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
void main(int argc, char *argv[])
|
|
||||||
{
|
{
|
||||||
int array[len] = { 5, 8 , 10 , 14 ,16 };
|
int array[len] = {5, 8, 10, 14, 16};
|
||||||
|
|
||||||
int position;
|
int position;
|
||||||
position = binarySearch(array, len, 5);
|
position = binarySearch(array, len, 5);
|
||||||
@ -44,5 +42,5 @@ void main(int argc, char *argv[])
|
|||||||
printf("The number %d exist in array at position : %d \n", 5, position);
|
printf("The number %d exist in array at position : %d \n", 5, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
23
searching/pattern_search/CMakeLists.txt
Normal file
23
searching/pattern_search/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
# Make sure YourLib is linked to each app
|
||||||
|
target_link_libraries( ${testname} function_timer )
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/searching/pattern")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -1,14 +0,0 @@
|
|||||||
CC = gcc
|
|
||||||
FLAG = -o
|
|
||||||
|
|
||||||
all: naive_search rabin_karp_search boyer_moore_search
|
|
||||||
|
|
||||||
naive_search : naive_search.c
|
|
||||||
$(CC) $(FLAG) naive_search naive_search.c
|
|
||||||
rabin_karp_search : rabin_karp_search
|
|
||||||
$(CC) $(FLAG) rabin_karp_search rabin_karp_search.c
|
|
||||||
boyer_moore_search: boyer_moore_search boyer_moore_search.c
|
|
||||||
$(CC) $(FLAG) boyer_moore_search boyer_moore_search.c
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm naive_search rabin_karp_search boyer_moore_search
|
|
@ -1,56 +1,47 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#define MAX 20
|
#define MAX 20
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
int i , arraySort[MAX] ={0} , isSort = FALSE, changePlace;
|
int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace;
|
||||||
|
|
||||||
|
/* For example
|
||||||
/* For example
|
|
||||||
Insertion random values in array to test
|
Insertion random values in array to test
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
for (i = 0; i < MAX; i++)
|
||||||
for(i = 0 ; i < MAX; i++)
|
|
||||||
{
|
{
|
||||||
arraySort[i] = rand()%101 ;
|
arraySort[i] = rand() % 101;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Algorithm of bubble methods */
|
/* Algorithm of bubble methods */
|
||||||
|
|
||||||
while(isSort)
|
while (isSort)
|
||||||
{
|
{
|
||||||
isSort = FALSE;
|
isSort = FALSE;
|
||||||
|
|
||||||
for( i = 0 ; i < MAX - 1 ; i++)
|
for (i = 0; i < MAX - 1; i++)
|
||||||
{
|
{
|
||||||
if(arraySort[i] > arraySort[i+1])
|
if (arraySort[i] > arraySort[i + 1])
|
||||||
{
|
{
|
||||||
changePlace = arratSort[i];
|
changePlace = arraySort[i];
|
||||||
arraySort[i] = arraySort[i+1];
|
arraySort[i] = arraySort[i + 1];
|
||||||
arraySort[i+1] = changePlace ;
|
arraySort[i + 1] = changePlace;
|
||||||
isSort = TRUE;
|
isSort = TRUE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See if it works */
|
/* See if it works */
|
||||||
|
|
||||||
for(i = 0 ; i < MAX; i++)
|
for (i = 0; i < MAX; i++)
|
||||||
{
|
{
|
||||||
printf("%d\n", arraySort[i]);
|
printf("%d\n", arraySort[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
23
sorting/CMakeLists.txt
Normal file
23
sorting/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
if(USE_OPENMP)
|
||||||
|
find_package(OpenMP)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
# Make sure YourLib is linked to each app
|
||||||
|
target_link_libraries( ${testname} function_timer )
|
||||||
|
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C)
|
||||||
|
if(OpenMP_C_FOUND)
|
||||||
|
target_link_libraries(${testname} OpenMP::OpenMP_C)
|
||||||
|
endif()
|
||||||
|
install(TARGETS ${testname} DESTINATION "bin/sorting")
|
||||||
|
|
||||||
|
endforeach( testsourcefile ${APP_SOURCES} )
|
@ -24,13 +24,13 @@ int findMax(int arr[], int n)
|
|||||||
|
|
||||||
for (maxElementIdx = 0, i = 0; i < n; ++i)
|
for (maxElementIdx = 0, i = 0; i < n; ++i)
|
||||||
if (arr[i] > arr[maxElementIdx])
|
if (arr[i] > arr[maxElementIdx])
|
||||||
maxElementIdx = i;
|
maxElementIdx = i;
|
||||||
|
|
||||||
return maxElementIdx;
|
return maxElementIdx;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sorts the array using flip operations
|
// Sorts the array using flip operations
|
||||||
int pancakeSort(int *arr, int n)
|
void pancakeSort(int *arr, int n)
|
||||||
{
|
{
|
||||||
// Start from the complete array and one by one reduce current size by one
|
// Start from the complete array and one by one reduce current size by one
|
||||||
for (int curr_size = n; curr_size > 1; --curr_size)
|
for (int curr_size = n; curr_size > 1; --curr_size)
|
||||||
@ -39,13 +39,13 @@ int pancakeSort(int *arr, int n)
|
|||||||
int maxElementIdx = findMax(arr, curr_size);
|
int maxElementIdx = findMax(arr, curr_size);
|
||||||
|
|
||||||
// Move the maximum element to end of current array if it's not already at the end
|
// Move the maximum element to end of current array if it's not already at the end
|
||||||
if (maxElementIdx != curr_size-1)
|
if (maxElementIdx != curr_size - 1)
|
||||||
{
|
{
|
||||||
// To move at the end, first move maximum number to beginning
|
// To move at the end, first move maximum number to beginning
|
||||||
flip(arr, maxElementIdx);
|
flip(arr, maxElementIdx);
|
||||||
|
|
||||||
// Now move the maximum number to end by reversing current array
|
// Now move the maximum number to end by reversing current array
|
||||||
flip(arr, curr_size-1);
|
flip(arr, curr_size - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,26 +53,29 @@ int pancakeSort(int *arr, int n)
|
|||||||
// Displays the array, passed to this method
|
// Displays the array, passed to this method
|
||||||
void display(int arr[], int n)
|
void display(int arr[], int n)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define N 50
|
||||||
|
|
||||||
// Driver program to test above function
|
// Driver program to test above function
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int arr[] = {23, 10, 20, 11, 12, 6, 7};
|
int arr[N];
|
||||||
int n = sizeof(arr)/sizeof(arr[0]);
|
for (int i = 0; i < N; i++)
|
||||||
|
arr[i] = rand() % (N << 1); /* random numbers from 0 to 2N */
|
||||||
|
|
||||||
printf("Original array: ");
|
printf("Original array: ");
|
||||||
display(arr, n);
|
display(arr, N);
|
||||||
|
|
||||||
pancakeSort(arr, n);
|
pancakeSort(arr, N);
|
||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n);
|
display(arr, N);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,24 +1,24 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
void stoogesort(int [], int, int);
|
void stoogesort(int[], int, int);
|
||||||
|
|
||||||
void main()
|
int main()
|
||||||
{
|
{
|
||||||
int arr[100], i, n;
|
int arr[100], i, n;
|
||||||
|
|
||||||
printf("How many elements do you want to sort: ");
|
printf("How many elements do you want to sort: ");
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
for (i = 0;i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
scanf(" %d", &arr[i]);
|
scanf(" %d", &arr[i]);
|
||||||
stoogesort(arr, 0, n - 1);
|
stoogesort(arr, 0, n - 1);
|
||||||
printf("Sorted array : \n");
|
printf("Sorted array : \n");
|
||||||
for (i = 0;i < n;i++)
|
for (i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void stoogesort(int arr[], int i, int j)
|
void stoogesort(int arr[], int i, int j)
|
||||||
{
|
{
|
||||||
int temp, k;
|
int temp, k;
|
||||||
|
@ -1,91 +1,92 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void swap (int *a,int *b)//To swap the variables//
|
void swap(int *a, int *b) //To swap the variables//
|
||||||
{
|
{
|
||||||
int t;
|
int t;
|
||||||
t= *a;
|
t = *a;
|
||||||
*a=*b;
|
*a = *b;
|
||||||
*b=t;
|
*b = t;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void merge(int a[],int l,int r,int n)//To merge //
|
void merge(int a[], int l, int r, int n) //To merge //
|
||||||
{ int *b = (int*)malloc(n*sizeof(int));
|
{
|
||||||
int c=l;
|
int *b = (int *)malloc(n * sizeof(int));
|
||||||
int p1,p2;
|
int c = l;
|
||||||
p1 = l;p2=((l+r)/2)+1;
|
int p1, p2;
|
||||||
while ((p1<((l+r)/2)+1) &&(p2<r+1))
|
p1 = l;
|
||||||
{ if(a[p1] <= a[p2])
|
p2 = ((l + r) / 2) + 1;
|
||||||
{ b[c++] = a[p1];
|
while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
|
||||||
p1++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{b[c++] = a[p2];p2++;}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
if (p2 == r+1)
|
|
||||||
{
|
{
|
||||||
while ((p1<((l+r)/2)+1))
|
if (a[p1] <= a[p2])
|
||||||
{
|
{
|
||||||
b[c++] = a[p1];
|
b[c++] = a[p1];
|
||||||
p1++;
|
p1++;
|
||||||
};
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
b[c++] = a[p2];
|
||||||
|
p2++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (p2 == r + 1)
|
||||||
|
{
|
||||||
|
while ((p1 < ((l + r) / 2) + 1))
|
||||||
|
{
|
||||||
|
b[c++] = a[p1];
|
||||||
|
p1++;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while ((p2<r+1))
|
while ((p2 < r + 1))
|
||||||
{
|
{
|
||||||
b[c++] = a[p2];
|
b[c++] = a[p2];
|
||||||
p2++;
|
p2++;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (c=l;c<r-l+1;c++)
|
for (c = l; c < r - l + 1; c++)
|
||||||
a[c] = b[c];
|
a[c] = b[c];
|
||||||
|
|
||||||
}
|
}
|
||||||
void mergesort(int a[],int n,int l,int r)
|
|
||||||
|
void merge_sort(int *a, int n, int l, int r)
|
||||||
{
|
{
|
||||||
if (r-l == 1 )
|
if (r - l == 1)
|
||||||
|
{
|
||||||
|
if (a[l] > a[r])
|
||||||
|
swap(&a[l], &a[r]);
|
||||||
|
}
|
||||||
|
else if (l == r)
|
||||||
{
|
{
|
||||||
if (a[l]>a[r])
|
|
||||||
swap(&a[l],&a[r]);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(l==r)
|
|
||||||
{}
|
|
||||||
else
|
else
|
||||||
{mergesort(a,n,l,(l+r)/2);
|
{
|
||||||
mergesort(a,n,((l+r)/2)+1,r);
|
merge_sort(a, n, l, (l + r) / 2);
|
||||||
merge(a,l,r,n);
|
merge_sort(a, n, ((l + r) / 2) + 1, r);
|
||||||
|
merge(a, l, r, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
int main(void) { //main function//
|
int main(void)
|
||||||
int *a,n,i;
|
{ //main function//
|
||||||
scanf("%d",&n);
|
int *a, n, i;
|
||||||
a = (int*)malloc(n*sizeof(int));
|
scanf("%d", &n);
|
||||||
for (i=0;i<n;i++)
|
a = (int *)malloc(n * sizeof(int));
|
||||||
{
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
scanf("%d",&a[i]);
|
|
||||||
|
scanf("%d", &a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
merge_sort(a, n, 0, n - 1);
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf(" %d", a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
mergesort(a,n,0,n-1);
|
|
||||||
for (i=0;i<n;i++)
|
|
||||||
{
|
|
||||||
|
|
||||||
printf(" %d",&a[i]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -259,7 +259,7 @@ void insert2(char *s)
|
|||||||
|
|
||||||
Tptr pp, *p;
|
Tptr pp, *p;
|
||||||
p = &root;
|
p = &root;
|
||||||
while (pp = *p)
|
while (pp == *p)
|
||||||
{
|
{
|
||||||
if ((d = *s - pp->splitchar) == 0)
|
if ((d = *s - pp->splitchar) == 0)
|
||||||
{
|
{
|
||||||
@ -390,7 +390,6 @@ void nearsearch(Tptr p, char *s, int d)
|
|||||||
nearsearch(p->hikid, s, d);
|
nearsearch(p->hikid, s, d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define NUMBER_OF_STRING 3
|
#define NUMBER_OF_STRING 3
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -1,42 +1,44 @@
|
|||||||
//sorting of array list using Radix sort
|
//sorting of array list using Radix sort
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||||
|
|
||||||
// Utility function to get the maximum value in ar[]
|
// Utility function to get the maximum value in ar[]
|
||||||
int MAX(int ar[], int size){
|
int MAX(int ar[], int size)
|
||||||
int i, max = ar[0];
|
{
|
||||||
for(i = 0; i<size; i++){
|
int i, max = ar[0];
|
||||||
if(ar[i]>max)
|
for (i = 0; i < size; i++)
|
||||||
max = ar[i];
|
{
|
||||||
}
|
if (ar[i] > max)
|
||||||
return max;
|
max = ar[i];
|
||||||
|
}
|
||||||
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Counting sort according to the digit represented by place
|
// Counting sort according to the digit represented by place
|
||||||
void countSort(int arr[],int n,int place)
|
void countSort(int arr[], int n, int place)
|
||||||
{
|
{
|
||||||
int i,freq[range]={0};
|
int i, freq[range] = {0};
|
||||||
int output[n];
|
int output[n];
|
||||||
|
|
||||||
// Store count of occurences in freq[]
|
// Store count of occurences in freq[]
|
||||||
for(i=0;i<n;i++)
|
for (i = 0; i < n; i++)
|
||||||
freq[(arr[i]/place)%range]++;
|
freq[(arr[i] / place) % range]++;
|
||||||
|
|
||||||
// Change freq[i] so that it contains the actual position of the digit in output[]
|
// Change freq[i] so that it contains the actual position of the digit in output[]
|
||||||
for(i=1;i<range;i++)
|
for (i = 1; i < range; i++)
|
||||||
freq[i]+=freq[i-1];
|
freq[i] += freq[i - 1];
|
||||||
|
|
||||||
// Build the output array
|
// Build the output array
|
||||||
for(i=n-1;i>=0;i--)
|
for (i = n - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
output[freq[(arr[i]/place)%range]-1]=arr[i];
|
output[freq[(arr[i] / place) % range] - 1] = arr[i];
|
||||||
freq[(arr[i]/place)%range]--;
|
freq[(arr[i] / place) % range]--;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the output array to arr[], so it contains numbers according to the current digit
|
// Copy the output array to arr[], so it contains numbers according to the current digit
|
||||||
for(i=0;i<n;i++)
|
for (i = 0; i < n; i++)
|
||||||
arr[i]=output[i];
|
arr[i] = output[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*This is where the sorting of the array takes place
|
/*This is where the sorting of the array takes place
|
||||||
@ -44,41 +46,48 @@ void countSort(int arr[],int n,int place)
|
|||||||
n --- Array Size
|
n --- Array Size
|
||||||
max --- Maximum element in Array
|
max --- Maximum element in Array
|
||||||
*/
|
*/
|
||||||
void radixsort(int arr[],int n,int max) //max is the maximum element in the array
|
void radixsort(int arr[], int n, int max) //max is the maximum element in the array
|
||||||
{
|
{
|
||||||
int mul=1;
|
int mul = 1;
|
||||||
while(max)
|
while (max)
|
||||||
{
|
{
|
||||||
countsort(arr,n,mul);
|
countSort(arr, n, mul);
|
||||||
mul*=10;
|
mul *= 10;
|
||||||
max/=10;
|
max /= 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]){
|
void display(int *arr, int N)
|
||||||
int n;
|
{
|
||||||
printf("Enter size of array:\n");
|
for (int i = 0; i < N; i++)
|
||||||
scanf("%d", &n); // E.g. 8
|
printf("%d, ", arr[i]);
|
||||||
|
putchar('\n');
|
||||||
printf("Enter the elements of the array\n");
|
}
|
||||||
int i;
|
|
||||||
int arr[n];
|
int main(int argc, const char *argv[])
|
||||||
for(i = 0; i < n; i++){
|
{
|
||||||
scanf("%d", &arr[i] );
|
int n;
|
||||||
}
|
printf("Enter size of array:\n");
|
||||||
|
scanf("%d", &n); // E.g. 8
|
||||||
printf("Original array: ");
|
|
||||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
printf("Enter the elements of the array\n");
|
||||||
|
int i;
|
||||||
int max;
|
int arr[n];
|
||||||
max = MAX(arr,n);
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
radixsort(arr, n, max);
|
scanf("%d", &arr[i]);
|
||||||
|
}
|
||||||
printf("Sorted array: ");
|
|
||||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
printf("Original array: ");
|
||||||
|
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||||
return 0;
|
|
||||||
|
int max;
|
||||||
|
max = MAX(arr, n);
|
||||||
|
|
||||||
|
radixsort(arr, n, max);
|
||||||
|
|
||||||
|
printf("Sorted array: ");
|
||||||
|
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,111 +1,68 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define ELEMENT_NR 20000
|
#define ELEMENT_NR 20000
|
||||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
const char *notation = "Shell Sort Big O Notation:\
|
const char *notation = "Shell Sort Big O Notation:\
|
||||||
\n--> Best Case: O(n log(n)) \
|
\n--> Best Case: O(n log(n)) \
|
||||||
\n--> Average Case: depends on gap sequence \
|
\n--> Average Case: depends on gap sequence \
|
||||||
\n--> Worst Case: O(n)";
|
\n--> Worst Case: O(n)";
|
||||||
|
|
||||||
void show_data(int arr[], int len)
|
void show_data(int arr[], int len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
printf("%3d ", arr[i]);
|
printf("%3d ", arr[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(int *a, int *b)
|
void swap(int *a, int *b)
|
||||||
{
|
{
|
||||||
int tmp;
|
int tmp;
|
||||||
|
|
||||||
tmp = *a;
|
tmp = *a;
|
||||||
*a = *b;
|
*a = *b;
|
||||||
*b = tmp;
|
*b = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shellSort(int array[], int len)
|
void shellSort(int array[], int len)
|
||||||
{
|
{
|
||||||
int i, j, gap;
|
int i, j, gap;
|
||||||
|
|
||||||
for (gap = len / 2; gap > 0; gap = gap / 2)
|
for (gap = len / 2; gap > 0; gap = gap / 2)
|
||||||
for (i = gap; i < len; i++)
|
for (i = gap; i < len; i++)
|
||||||
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap)
|
||||||
swap(&array[j], &array[j + gap]);
|
swap(&array[j], &array[j + gap]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
int main(int argc, char *argv[])
|
||||||
* Optimized algorithm - takes half the time as other
|
{
|
||||||
**/
|
int i;
|
||||||
void shell_sort2(int array[], int LEN)
|
int array[ELEMENT_NR];
|
||||||
{
|
int range = 500;
|
||||||
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
int size;
|
||||||
const int gap_len = 8;
|
clock_t start, end;
|
||||||
int i, j, g;
|
double time_spent;
|
||||||
|
|
||||||
for (g = 0; g < gap_len; g++)
|
srand(time(NULL));
|
||||||
{
|
for (i = 0; i < ELEMENT_NR; i++)
|
||||||
int gap = gaps[g];
|
array[i] = rand() % range + 1;
|
||||||
for (i = gap; i < LEN; i++)
|
|
||||||
{
|
size = ARRAY_LEN(array);
|
||||||
int tmp = array[i];
|
|
||||||
|
show_data(array, size);
|
||||||
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
|
start = clock();
|
||||||
array[j] = array[j - gap];
|
shellSort(array, size);
|
||||||
array[j] = tmp;
|
end = clock();
|
||||||
}
|
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
printf("Data Sorted\n");
|
||||||
for (i = 0; i < LEN; i++)
|
show_data(array, size);
|
||||||
printf("%s\t", data[i]);
|
|
||||||
#endif
|
printf("%s\n", notation);
|
||||||
}
|
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
return 0;
|
||||||
{
|
}
|
||||||
int i;
|
|
||||||
int array[ELEMENT_NR];
|
|
||||||
int array2[ELEMENT_NR];
|
|
||||||
int range = 500;
|
|
||||||
int size;
|
|
||||||
clock_t start, end;
|
|
||||||
double time_spent;
|
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
for (i = 0; i < ELEMENT_NR; i++)
|
|
||||||
{
|
|
||||||
array[i] = rand() % range + 1;
|
|
||||||
array2[i] = array[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
size = ARRAY_LEN(array);
|
|
||||||
|
|
||||||
show_data(array, size);
|
|
||||||
start = clock();
|
|
||||||
shellSort(array, size);
|
|
||||||
end = clock();
|
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
|
||||||
|
|
||||||
printf("Data Sorted\n");
|
|
||||||
show_data(array, size);
|
|
||||||
|
|
||||||
printf("%s\n", notation);
|
|
||||||
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
|
|
||||||
|
|
||||||
printf("--------------------------\n");
|
|
||||||
start = clock();
|
|
||||||
shell_sort2(array2, size);
|
|
||||||
end = clock();
|
|
||||||
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
|
||||||
|
|
||||||
printf("Data Sorted\n");
|
|
||||||
show_data(array2, size);
|
|
||||||
|
|
||||||
printf("%s\n", notation);
|
|
||||||
printf("Time spent sorting: %.4g ms\n", time_spent * 1e3);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
81
sorting/shell_Sort2.c
Normal file
81
sorting/shell_Sort2.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "function_timer.h"
|
||||||
|
|
||||||
|
#define ELEMENT_NR 20000
|
||||||
|
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
||||||
|
void show_data(int arr[], int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
printf("%3d ", arr[i]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *a, int *b)
|
||||||
|
{
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
tmp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optimized algorithm - takes half the time as other
|
||||||
|
**/
|
||||||
|
void shell_sort(int array[], int LEN)
|
||||||
|
{
|
||||||
|
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||||
|
const int gap_len = 8;
|
||||||
|
int i, j, g;
|
||||||
|
|
||||||
|
for (g = 0; g < gap_len; g++)
|
||||||
|
{
|
||||||
|
int gap = gaps[g];
|
||||||
|
for (i = gap; i < LEN; i++)
|
||||||
|
{
|
||||||
|
int tmp = array[i];
|
||||||
|
|
||||||
|
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
|
||||||
|
array[j] = array[j - gap];
|
||||||
|
array[j] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
for (i = 0; i < LEN; i++)
|
||||||
|
printf("%s\t", data[i]);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int array[ELEMENT_NR];
|
||||||
|
int range = 500;
|
||||||
|
int size;
|
||||||
|
double time_spent;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
for (i = 0; i < ELEMENT_NR; i++)
|
||||||
|
array[i] = rand() % range + 1;
|
||||||
|
|
||||||
|
size = ARRAY_LEN(array);
|
||||||
|
|
||||||
|
function_timer *timer = new_timer();
|
||||||
|
|
||||||
|
show_data(array, size);
|
||||||
|
start_timer(timer);
|
||||||
|
shell_sort(array, size);
|
||||||
|
time_spent = end_timer(timer);
|
||||||
|
|
||||||
|
printf("Data Sorted\n");
|
||||||
|
show_data(array, size);
|
||||||
|
|
||||||
|
printf("Time spent sorting: %.4g s\n", time_spent);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user