Showing posts with label help. Show all posts
Showing posts with label help. Show all posts

Thursday, October 2, 2014

Finding nth prime number (large number) (Optimization)

This was something handy I learned. Actually the problem is pretty easy, but when finding the nth prime where n>1000000 it takes way to long in the conventional way youre thinking. Here is an optimized solution to it, that reduces the time complexity. If you have any doubts, ask!

I think you can even optimize it further by doing p=p+2 and num=num+2. Try it out and see

def prime(chk):
if(chk%2==0):
return False
else:
p=3
while(p<chk**0.5+1):
if(chk%p==0):
return False
p=p+1
return True



def is_prime(x):
count=2
num=4
while(count<x):
if prime(num):
count+=1
num1=num
num=num+1
return num1

result=is_prime("Enter the nth term here")
print result

Tuesday, September 23, 2014

Program to print out the letters present in one word and absent in the other.

If you have any doubts, please do ask.

#include<stdio.h>
main()
{
    int i,j,flag=0;
char name1[10],name2[10];
scanf("%s",name1);
scanf("%s",name2);
for(i=0;i<strlen(name2);i++)
{
for(j=0;j<strlen(name1);j++)
{
if(name2[i]==name1[j])
flag=1;
}
if(flag==0)
{printf("%c",name2[i]);

}
else
    flag=0;
}}

Program to print out numbers separated by stars with the occurrence defined by the number printed.

This will print out something like this
1
2*2
3*3*3
4*4*4*4
and then the reverse image.
If you have any doubt, please do ask.

#include<stdio.h>
main()
{
int i=1,j=2,z=4,k;
printf("%d\n",i);
for(k=0;k<3;k++)
{
for(i=2;i<z-1;i++)
{
printf("%d*",j);
}
printf("%d\n");
j++;
z++;
}
j=4;
for(k=0;k<3;k++)
{
for(i=2;i<z-2;i++)
{
printf("%d*",j);
}
printf("%d\n");
j--;
z--;
}
printf("%d",1);
}

String reversal without string.h

This is a program to reverse a string without the string.h header. If you have any doubts, please do ask.

#include<stdio.h>
main()
{
int i,count=0,j=0;
char string[10],final[10];
scanf("%s",string);
for(i=0;string[i]!='\0';i++)
count++;
count--;
for(i=count;i>=0;i--)
{
final[j]=string[i];
j++;
}
final[j]='\0';
printf("%s",final);
}

Saturday, August 9, 2014

Program for quick sort

Program to implement quick sort. If you need help, comment below

#include<stdio.h> int a[20],b[20],n,z; void quick(int x[],int first,int last) { int pivot; if(first<last) { pivot=partition(x,first,last); quick(x,first,pivot-1); quick(x,pivot+1,last); } } int partition(int x[],int first,int last) { int pivot,temp,i,j; pivot=first; i=first; j=last; while(i<j) { while((x[i]<=x[pivot])&&(i<last)) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } for(z=1;z<=n;z++) printf("%d\t ",a[z]); printf("\n"); } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; return j; } int main() { int i,j,c,ch; printf("\nQuicksort"); printf("\nEnter the no. of elements in the array:\t"); scanf("%d",&n); printf("\nEnter the elements into the array:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); quick(a,1,n); printf("\nAfter sorting:\n"); for(i=1;i<=n;i++) printf("%d ",a[i]); }

Program for merge sort.

Program for merge sort. If you need help, feel free to ask.

#include<stdio.h> void mergesort(int x[20],int n) { int temp[20],i,j,k,l1,l2,size,u1,u2,a; size=1; while(size<n) { l1=0; k=0; while(l1+size<n) { l2=l1+size; u1=l2-1; u2=((l2+size-1<n)?l2+size-1:n-1); for(i=l1,j=l2;i<=u1&&j<=u2;k++) { if(x[i]<=x[j]) { temp[k]=x[i]; i++; } else { temp[k]=x[j]; j++; } } for(;i<=u1;k++) { temp[k]=x[i]; i++; } for(;j<=u2;k++) { temp[k]=x[j]; j++; } l1=u2+1; } for(i=l1;k<n;k++) temp[k++]=x[i]; for(i=0;i<n;i++) x[i]=temp[i]; size=size*2; printf("\n"); for(a=0;a<n;a++) printf("%d ",x[a]); printf("\n"); } } main() { int x[20],n,i; printf("\nEnter the value of the number of elements you wish to enter\n"); scanf("%d",&n); printf("\nEnter values of the elements you wish to sort"); for(i=0;i<n;i++) scanf("%d",&x[i]); printf("\nBefore Sorting this is how it looks\n:"); for(i=0;i<n;i++) printf(" %d",x[i]); mergesort(x,n); }

Program for heap sort

Program for Heap Sort. Kinda tricky program. If you need help, comment below

#include<stdio.h> int tree[20],n,ah[20],c=0; void build(int m,int item) { int ptr,par,f=0; m=m+1; ptr=m; while(ptr>1) { par=ptr/2; if(item<=tree[par]) { tree[ptr]=item; f=1; break; } tree[ptr]=tree[par]; ptr=par; } if(f==0) tree[1]=item; } void delheap(int k,int m) { int left,right,ptr,last,i; ah[k]=tree[1]; last=tree[m]; m=m-1; ptr=1; left=2; right=3; while(right<=m) { if((last>=tree[left])&&(last>=tree[right])) { tree[ptr]=last; return; } if(tree[right]<=tree[left]) { tree[ptr]=tree[left]; ptr=left; } else { tree[ptr]=tree[right]; ptr=right; } left=2*ptr; right=left+1; } if((left==m)&&(last<tree[left])) { tree[ptr]=tree[left]; ptr=left; } tree[ptr]=last; c++; printf("\n\nPass %d: ",c); for(i=1;i<=m;i++) printf("%d ",tree[i]); } main() { int i,j; printf("\nEnter total number of elements:"); scanf("%d",&n); printf("\nEnter elements to be inserted : "); for(i=1;i<=n;i++) scanf("%d",&tree[i]); for(i=1;i<n;i++) build(i,tree[i+1]); printf("\n\nHeap : "); for(i=1;i<=n;i++) printf("%d ",tree[i]); for(i=n,j=1;i>1;i--,j++) delheap(j,i); if(i==1) ah[n]=tree[1]; printf("\n\nHeap sort : "); for(i=1;i<=n;i++) printf("%d ",ah[i]); }

Program to count number of occurrences of a word in a sentence.

Simple C program to count the number of occurrences of a word in a sentence. If you need help, feel free to ask.


#include<stdio.h>
#include<string.h>
main()
{
  int len,c=0,i=0,j=0,ans=0;
  char str1[50],str2[20],new[20];
  printf("\n enter the text: ");
  gets(str1);
  printf("\n enter the word: ");
  gets(str2);
  len=strlen(str1);
  for(i=0;i<=len;i++)
   {
     if(str1[i]!=' ')
      {
       new[j]=str1[i];
       j++;
      }
     if(str1[i]==' '||str1[i]=='\0')
       {
           new[j]='\0';
           j=0;
           ans=strcmp(new,str2);
           if(ans==0)
             c++;
        }
    }
   printf("\n the number of occurence is %d\n",c);
}

Largest and second largest of all rows and columns of a matrix.

This is a C program to find the largest and second largest element of all the rows and columns of a matrix. If you have any doubts, please feel free in asking.

#include<stdio.h>
main()
{
 int a[50][50],size,n,m,i,j,large,slarge;
 printf("enter size of matrix(row and column):");
 scanf("%d %d",&n,&m);
 printf("\n enter the elements:");
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
   scanf("%d",&a[i][j]);
 }
 printf("\n the inputed matrix is:\n");
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    printf("\t%d  ",a[i][j]);
   printf("\n");
  }
 for(i=0;i<n;i++)
  {
    slarge=a[i][0];
    large=a[i][0];
    for(j=0;j<m;j++)
     {
       if(large<a[i][j])
         large=a[i][j];
     }
    printf("\n the largest of row%d is %d",i+1,large);
    for(j=0;j<m;j++)
     {
       if(a[i][j]!=large)
       {
        if(slarge<a[i][j])
          slarge=a[i][j];
       }
     }
    printf("\nthe seceond largest of row%d is %d",i+1,slarge);
   }
 for(j=0;j<m;j++)
  {
    slarge=a[0][j];
    large=a[0][j];
   


for(i=0;i<n;i++)
    {
      if(large<a[i][j])
        large=a[i][j];
    }
    printf("\n the largest of column %dis %d",j+1,large);
    for(i=0;i<n;i++)
     {
       if(a[i][j]!=large)
        {
          if(slarge<a[i][j])
            slarge=a[i][j];
        }
     }
    printf("\nthe second largest of column %dis %d\n",j+1,slarge);
  }
 }