nitin1 15 Master Poster

people like you has made daniweb.com valuable. thank you sir!! it is really damn helpful to me. saved my many hours. thanks. Can you tell me one thing here that if i want to have a server and many clients and one client can communicate with other client thorugh server. then how can i implement server so as to do achive the task ? any idea ?

nitin1 15 Master Poster

can you please elaborate it more ? how can i use popen and fork here ?

nitin1 15 Master Poster

actually, I just want the idea here. I have a server, a client . client will request to display the files names in its directory(server's current directory). server will send the list.

my thinking : system command will be used here. when clinet enter "-ls" then server will have system() call and will get all the file names. but my problem is that how it will send these files name to the client ? can you please give me idea ? thanks.

nitin1 15 Master Poster

i dont know what these versions are... i do coding on online judges like spoj etc... they have this division... so i was asking why are they giving this division.. there is no division in python, java, C , or any other,,, but why in this one only

nitin1 15 Master Poster

i have started using C++ these days. Initially , I have done coding alot in C language. I have seen that on my onliune judges , there are 2 different versions. Why they have given 2 versions to compile the code ? Can you tell ? thanks if you can help me..

nitin1 15 Master Poster

but i was asking the time when you are preparing for exams ?

nitin1 15 Master Poster

if i also write the same code without indentaion , then me also will get the compilation error. TIP: if you again want to write this type of code which have so many if else conditions then try to wrute the indented code. use codeblocks or notepad++ or dev c++ they will indent for you while you are writing. thanks.

nitin1 15 Master Poster

It may or it may not. depends you have all the the header file which it want. if you have , then no point that it will not compile, and if not, then there is not any question of running ;) simple!

nitin1 15 Master Poster

yes, i have read it. but how will i change it. i know the answer , i dont know how to change it. thanks

nitin1 15 Master Poster

lol... a big lol @sahil... hahahahah

nitin1 15 Master Poster

how do you make your cool or stress free in exams days ?

nitin1 15 Master Poster

sir, then what is will print as the ascii value of 1010 ? it is 10 decimal. so what should be the output ? in by book, it is given that ascii of 45 is 3435. as 0--> 30h and 4-->34 & 5--> 35 . i think.

nitin1 15 Master Poster

but my question is that like i have i/p a binary number say, 1010 (10 in decimal), then what will be its ascii which ahould be in o/p ? i am using MASM , which have many registers and i have to output value in one of them. in registers , we need to o/p the value in hex. right ?

P.S i am telling something(resgiters) and asking(remaining) something also.

nitin1 15 Master Poster

i am using masm to write assebmly language codes. can you please tell that how can i change binary numbers to its ascii ? like 10 is given what will be its ascii value ? i know that 30h is ascii value of 0 ans so on. please help.

nitin1 15 Master Poster

see the tutorials... it has well developed tutorials. and also , still you dont understand the logic, then see the solutions over there. many people has solved this problem, even me also :p :-D

nitin1 15 Master Poster

server .c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<string.h>

int main()
{
    int sockid,newsockid;
    int processid, client_add;
    char s[10];
    struct sockaddr_in myaddr,clientaddr;    


    sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid<0)
    perror("socket");

    memset(&myaddr,0,sizeof myaddr);
    myaddr.sin_port=htons(8765);
    myaddr.sin_family=AF_INET;
    myaddr.sin_addr.s_addr=htonl(INADDR_ANY);

    printf("socket id is:%d\n",sockid);

    if((bind(sockid,(struct sockaddr*)&myaddr,sizeof myaddr))==-1)
    perror("bind");

    if(listen(sockid,10)==-1)
    perror("listen");

    client_add=sizeof clientaddr;
    printf("size of client_add is: %d\n",client_add);

    newsockid=accept(sockid,(struct sockaddr*)&clientaddr,&client_add);
    if(newsockid==-1)
    perror("accept");

     char * buffer=(char*)(malloc(sizeof(char)*1024));
     int buffsize=1024,i=0;
  int l=0;char c;char msg[200];
     while(i<10)
     {
                 recv(newsockid,buffer,buffsize,0);
                   l=0;

                 fprintf(stdout, "Client : %s", buffer);

                  printf("\n");
                 printf("server: ");
                 l=0;
                while((c=getchar())!='\n')
                {
            msg[l++]=c;
                }

                msg[l]='\0'; l++;
                send(newsockid,msg,l,0);
                   i++;            

     }


    printf("server program ended\n");
    close(sockid);
    return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<string.h>

int main()
{

     printf("this is a client program\n");
     struct sockaddr_in myaddr,clientaddr;
     int newsockid;
     int len;
     int sockid,client_add;
     sockid=socket(AF_INET,SOCK_STREAM,0);

     if(sockid==-1)
     perror("socket");

     memset(&myaddr,0,sizeof myaddr);
     myaddr.sin_port=htons(8765);
     myaddr.sin_family=AF_INET;
     myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

     len=sizeof myaddr;
      if((bind(sockid,(struct sockaddr*)&myaddr,sizeof myaddr))==-1)
    perror("bind");
     int p=connect(sockid,(struct sockaddr*)&myaddr,len);

     if(p==-1)
     perror("connect");

     char msg[200];
     int i=0;
     char c;int l=0; char *buffer=(char *)(malloc(sizeof(char)*200)); int buffsize;
     while(i<10)
     {l=0;
                printf("Client: ");
                while((c=getchar())!='\n')
                {
            msg[l++]=c;
                }

                msg[l]='\0'; l++;
                send(sockid,msg,l,0);

                fflush(stdin);
                newsockid=accept(sockid,(struct sockaddr*)&clientaddr,&client_add);
                recv(newsockid,buffer,buffsize,0);
                   l=0;

                 fprintf(stdout, "server: %s", buffer);

                  printf("\n");
                i++;
     }


     close(sockid);

     return 0;
}

thanks.

nitin1 15 Master Poster

Can you please give me hints or can you teach me how can i convert my simple client server program(in which only client can send any number of messages it want to server) to chat server client program ? I am doing this by trivial way, but still only server is receving but not client whichever thing server is sending. I have done all these things with the help of socket programming and in C language. thanks if you can help me.

nitin1 15 Master Poster

what are they trying to say in this paragraph , I have read it 10 times, but not getting what they want to teach me here:

link is this. : http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=primeNumbers

and the para which is just before the euler totient theorem. am copying here also.

Or consider a scenario where you are asked to calculate a function Answer(x, y), with x and y both integers in the range [1, n], 1 ≤ n ≤ 50000. If you know Answer(x, y), then you can easily derive Answer(kx, k*y) for any integer k. In this situation you want to know how many values of Answer(x, y) you need to precalculate. The function Answer is not symmetric.

For example, if n = 4, you need to precalculate 11 values: Answer(1, 1), Answer(1, 2), Answer(2, 1), Answer(1, 3), Answer(2, 3), Answer(3, 2), Answer(3, 1), Answer(1, 4), Answer(3, 4), Answer(4, 3) and Answer(4, 1).

The solution here is to let res(i) be the minimum number of Answer(x, y) to precalculate, where x, y Î{1, …, i}. It is obvious that res(1) = 1, because if n = 1, it is enough to know Answer(1, 1). Let we know res(i). So for n = i + 1 we need to find Answer(1, i + 1), Answer(2, i + 1), … , Answer(i + 1, i + 1), Answer(i + 1, 1), Answer(i + 1, 2), … , Answer(i + 1, i).

The values Answer(j, i + 1) and Answer(i + 1, j), j …

nitin1 15 Master Poster

yes, it is a spoj,codechef,UVA, interviewstreet, codeforces and many others' question. but variation of this you can get at any site. Si of you can improve , i think it will be damn helpful for me :-) thanks and yeah, i have calculated it once only.

nitin1 15 Master Poster

lol. it's 4 bytes. ;)

nitin1 15 Master Poster
#define MAX 100000000

int primes[MAX/10];


void sieve()
{
      int i,j,k=0;
      int p=sqrt(MAX);

      for(i=3;i<=p;i+=2)
      {
            if(a[i]==0) 
             for(j=i*i;j<=MAX;j+=i)
             {
                    a[j]=1;
             }
      }

      primes[k++]=2;

      for(i=3;i<=MAX;i+=2)
      {
           if(a[i]==0)
           primes[k++]=i;
      }

      return;
}

This is what i am using right now, i want to optimize it more. how can i ? It is the code which i was talking about. thanks.

nitin1 15 Master Poster

hello, Can anyone please tell me the most efficient know till date so as to find the prime numbers up to 10^8 or higher. I am using sieve and using odd number optimization with it also. but it is not too much effiecent which i want. Can you please any optimization or any other way so as to calculate all prime numbers uptp 10^ 8 pr higher.

P.S I wanto calculate as i am doing project on cryptgraphy. thanks in advance if you can help me in this :-)

nitin1 15 Master Poster

Can any one please help me to optimize the sieve to find the primes upto 10^8 ? I want very efficient algorithm to find all primes till 10^8 for cryptsystem. i have implemented the naive sieve which is taking too much time. please sgare some classic approach with me. i will be thankful to u. thanks .

nitin1 15 Master Poster
#include<stdio.h>
#include<limits.h>


#define num 86090000
int a[5000000]={0};
short int s[99999999];


void sieve()
{
     int i,j,p=sqrt(num);
     int k=0;

     a[k++]=2;

     for(i=3;i<=num;i=i*i)
     {             
           s[i]=1;               
     }
     s[3]=0;
     for(i=5;i<=num;i=i*i)
     {
          s[i]=1;
     }

     s[5]=0;
     for(i=7;i<=num;i=i*i)
     {
          s[i]=1;
     }
     s[7]=0;
     for(i=11;i<=num;i=i*i)
     {
          s[i]=1;
     }
     s[11]=0;
     for(i=13;i<=num;i=i*i)
     {
          s[i]=1;
     }
     s[13]=0;
     for(i=17;i<=num;i=i*i)
     {
          s[i]=1;
     }

     s[17]=0;
     for(i=19;i<=p;i+=2)
     {
             if(s[i]==0)  
             {
                      // a[k++]=i;           
              for(j=i*i;j<=num;j+=i)
              {
                   s[j]=1;

              }
             }        
     }

     for(i=3;i<=num;i+=2)
     {
            if(s[i]==0)                 
            a[k++]=i;
     }

     printf("%d",k);
     return;


}

int main()
{
    int n=0;
    int q=0;

    sieve();

    scanf("%d",&q);

    while(q--)
    {
         scanf("%d",&n);

         printf("%d\n",a[n-1]);




   }
    return 0;
}

very awkward.. But it is giving segmentaion fault. in the all loops of the sieve() function. can you find how ?

nitin1 15 Master Poster

hmm sir! you are right! but what can we do in this if facebook like thing is still asking these type of questions ? :-| SO can you help how bit wise operators are helping in this ?

nitin1 15 Master Poster

@ancient dragon please read my last line. it is giving time limit excedded. so i want any other effiecent way.

@deceptikon lol. it's not a assignment. It is asked by facebook this year in the first round ;) so i think it is not that much bad question like you thinking it ;)

nitin1 15 Master Poster

i have to do division without divison/multiplication/mod operator. how can i ?

 int divide(int dividend, int divisor) {

        int p=1;

        if(dividend<0)
        {
             p*=-1;
             dividend*=-1;
        }

        if(divisor<0)
        {p*=-1;
           divisor*=-1;
        }

        while(dividend>=divisor)
        {
             dividend-=divisor;
        }

        return dividend*p;

    }

i have done this, but this giving time limit exceeded when input is 1234567890,1. how can i do it efficiently ?

nitin1 15 Master Poster

char

nitin1 15 Master Poster

iamthwee can you explain how ?

nitin1 15 Master Poster

Can anyone exactly tell that how do we decide the complexity in this case.
like nlogn +n , then the overall complexity is ?
like, if O(f(n)+g(n)) can be equal to max(f(n),g(n)) ? Can we say this ? thanks

nitin1 15 Master Poster
int main()
{
     int i=5;

     printf("%u",&i);

     return 0;
}

will the address which it will print is physical address of that variable or logical address ? I may not be clear in the defination of physical and logical address. thanks if you can help me ;)

nitin1 15 Master Poster

@bigpaw you are totally wrong. 100 % wrong!!

nitin1 15 Master Poster

You have 9 horses and you have to select fastest 3. You can make a race of 3 horses in one time. So how many time you have to make a race? thanks :-)

nitin1 15 Master Poster

this is a second issue! firstly tell me that how to find the number of increasing sequences of length k in n numbers, i mean how to modify this code a little so as to achieve the goal. later , i will think to optimize it. thanks

nitin1 15 Master Poster

are you trying to simplify my this code or are you giving answer of my question ? I didnt get it sorry to say this!

nitin1 15 Master Poster
#include<stdio.h>

int dp[20];

int lis(int a[],int n)
{
    int i,j,sum=0;
    dp[0]=1;
    for(i=0;i<6;i++)
    {
         for(j=i+1;j<n;j++)
         {
              if(a[j]>=a[i])
              {
                    dp[j]=dp[j-1]+1;        
              }
              else
              dp[j]=dp[j-1];
         }
    }
}


int main()
{
    int a[]={ 5, 3, 4, 8, 6, 7};
    int i=0;

    lis(a,6);

    for(i=0;i<6;i++)
    printf("%d ",dp[i]);
    getch();
    return 0;
}

hi , this is the code to print Longest increasing sequence in the given n numbers. but i want a little bit change in this that i want to tell the number of increasing sequqnces of length k , say 3 in this case. then what and where should i add something so as to do this. thanks

nitin1 15 Master Poster

can you explain how are you doing i=10, j=1 and multiply i by 10 each time thing ? what is your number in your explainations ?

secondly, please explain that ahy should i read till sqrt(n)-x ? what is x in this ?

nitin1 15 Master Poster

x^2 + S(x)*x - n=0 where n can be till 10^8 interger . and s(x) is the sum of the digits of the x integer. we have to print the value of x for which this equation holds otherwise print -1. my answer is taking too much time with brute force. can anyone give any hint ?

nitin1 15 Master Poster

;) yeah! no worries :) it's good to see that your problem is solved

nitin1 15 Master Poster

@ayush I have said you na that daniweb is the best place for your problems and see they all are helping alot. so keep update from here and ya! i have checked it solve() has problem. use a better algo to avoid that. thanks P.S (your senior :p)

nitin1 15 Master Poster

exactly, my thought was just like this, but my prob was that should i use a 3D Dp here ? because for n,k and one for 1 or 0 ?

nitin1 15 Master Poster

ohh! on what basis thing have you thought that it is a homwork ? have you ever seen any teacher giving homwork from a online judge ?

nitin1 15 Master Poster

here is a DP problem which i am solving from 2 days now. and i am finding a damn difficult. can you please tell what is overlaaping subproblem and what is optimal substructure in this problem. then i will try it again. any help will be appreciated. thanks

http://www.spoj.pl/problems/GNYR09F/

thanks

nitin1 15 Master Poster

actually, I want to improve my number theory concepts more and more as i need it in much extent. is there any good website or any book which can help me so as to make my needs? i am not weak or something like that in number theory, but still i wana improve it as much as i can till this year ends. thanks for any help in advance.

nitin1 15 Master Poster
#include<stdio.h>
#define num 10000000
int a[10000009]={0};

void sieve()
{
     int i,j;
       for(i=2;i<=3164;i++)          //loop till sqrt of largest number 10^7     
       {
              if(a[i]==0)                  // all primes are marked 0
              {
                   for(j=i*i;j<=num;j+=i)
                   {
                           if(a[j]==0)             
                            a[j]=i;        // if i is prime then all it's multiples pos have value of i   
                   }
              }
       }

       return;      // a[] is storing each nummber's smallest prime number which can divide it
}

int main()
{
     int n=0;
     a[1]=0;
     sieve();

     while(scanf("%d",&n)!=EOF)
     {
            printf("1");

            if(n==1)
            {
                printf("\n");
                continue;    
            }

            int k=n;

            while(a[k]!=0)                    // while it doesnt reach a prime number
            {
                printf(" x %d",a[k]);   
                k/=a[k];
            }
            printf(" x %d",k);               //print that prime number

            printf("\n");
     }    

     return 0;
}

hello, this is a code to print the factorization of a number upto 10^7 efficiently than brute force(much).So i want to optimize it more further. any one can make it more optimize so as to run it in better time ? thanks

nitin1 15 Master Poster

don't think this stupid thing ever! it's good to tell something may be that is not asked by me. it's better to tell some important things. and please i feel very low as you saying that you regret after telling me something! I am not so bad, my highness! sometimes, my questions are so bad that you wil laugh but still you must bot regret that. thanks

nitin1 15 Master Poster

why are you regretting now ? what happenned if you have mentioned that ? @deceptikon

nitin1 15 Master Poster

so , in which case i can't rely on it ? is there any cases in which i must not reply on this function ? thanks for this great info

nitin1 15 Master Poster

i know you have read it from dennis, i was going to guess it ;) i have also read it 2 times till date. ;) read it thoroughly, just watching your comment, i was also confused. thanks

nitin1 15 Master Poster

what ? qsort() doesn't mean it is qsort() ? really ? don't it take nlogn time to sort the arrray ? please clearify.

secondly, ** any reasonable implementation will use an O(nlogn) algorithm, so the lack of a guarantee is mostly just academic. But it's good to be aware of it** will you please xplain this line ? thanks in advance.