C Programmers Help Me!!!!

chamidilk

Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    Lab6-E.1-menna prime numbers

    1st solution(dz 1 z mine)-dz prog z de fastest 1 in investigating a prime

    Code:
    #include<stdio.h>
    main(){
    	int i,s,ip;
    	scanf("%d",&s);
    	while(s!=-1){
    		if(s==2)printf("prime\n");
    		else if(s%2==0);
    		else {
    			if(s!=1){
    				ip=1;
    				for(i=2;i*i<=s;i++){
    					if(s%i==0){
    						ip=-1;
    						break;
    					}
    				}
    				if(ip==1)printf("prime\n");
    			}
    		}
    		scanf("%d",&s);
    	}
    }

    2nd solution which u all like very much

    Code:
    #include<stdio.h>
    main(){
    	int s,i,j,ip;
    	scanf("%d",&s);
    	while(s!=-1){
    		if(s==2||s==3)printf("prime\n");
    		else{
    			if(s!=1){
    				ip=1;
    				for(j=3;j<s;j+=1){
    					if(s%j==0){
    						ip=-1;break;
    					}
    				}
    				if(ip==1)printf("prime\n");
    			}
    		}
    		scanf("%d",&s);
    	}
    }
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    C++ version, C paranai ne meka kochara simpla plan ekak da? :P

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        bool isprime;
        cin>>n;
        isprime=true;
        i=abs(n); //neglect negative numbers
        n=i;
        while (i>2) {
              i--;
              if (n%i==0) {isprime=false; break;} ;
              }         
        if (isprime==1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     
    Last edited:

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    menna ithiri progs2E.2 & E.3

    Exercise 2-

    Code:
    #include<stdio.h>
    main(){
    	int i,j,k;
    	scanf("%d",&k);
    	while(k!=-1){
    		for(i=1;i<k;i++){
    			if(k%i==0)j+=i;
    		}
    		if(j==k)printf("perfect\n");
    		j=0;
    		scanf("%d",&k);
    	}
    }

    Exercise 3-

    Code:
    #include<stdio.h>
    main(){
    	int i,j;
    	for(j=0;j<5;j++){
    		for(i=6;i>j;i--)printf("$");
    		printf("\n");
    	}
    	printf("$\n");
    	for(j=0;j<5;j++){
    		for(i=0;i<j+2;i++)printf("$");
    		printf("\n");
    	}
    }
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    please

    madurax86 said:
    C++ version, C paranai ne meka kochara simpla plan ekak da? :P

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        bool isprime;
        cin>>n;
        isprime=true;
        i=abs(n); //neglect negative numbers
        n=i;
        while (i>2) {
              i--;
              if (n%i==0) {isprime=false; break;} ;
              }         
        if (isprime==1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    i appreciate your postings here
    please bro
    cn u please correct ur code and put t back here
    my uni mates will be confused in ur prog
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    i appreciate your postings here
    please bro
    cn u please correct ur code and put t back here
    my uni mates will be confused in ur prog

    correct what? it works fine with mingw32 compiler its C++ not C :)
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    r u sure check t back n tell me
    sure dath kiyala ahanawa

    df.jpg
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    check whether 2 is a prime or not using your prog .......
    I am sure your prog hz a mistake

    I didnt change anything it works well man :P i edited the code some while ago(before you said it has a bug) to make it shorter after posting in EK u may have copied the program then copy it now and see

    I copied the code from EK then compiled a new project same result(this is the exact code that u quoted)
    dzf-2.jpg


    Just by looking at the code you can say that 2 is never being passed thru the algorithm only numbers greater than 2, i=2 is bypassed and returns prime, its the same for 1 too
     
    Last edited:

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    hmmmmm
    fine ur prog z correct ....

    sorry .......i dint get tht de value n .... actually by mistake t wz commented
    thts y t did an error

    bt still thr z n error

    t gives u 1 is a prime ...
    z t correct my dear ???????
    1 is nt a prime although ts facors r 1 n tself only

    n next error z u r investigating whether even numbers r primes xcluding 2 .......

    bt u r doing a very gud contribution 2 my C knowledge n my mates ....
    keep posting
     
    Last edited:

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    menna oyage CPP prog eka modify kala - poddak speedup wei

    madurax86 said:
    C++ version, C paranai ne meka kochara simpla plan ekak da? :P

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        int isprime;
        cin>>n;
        isprime=true;
        i=abs(n); //neglect negative numbers
        n=i;
        while (i>2) {
              if(n%2==0){isprime=-1;break;}
              i--;
              if (n%i==0) {isprime=false; break;} ;
              }         
        if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }


    meke 1 prime ekak widiyata ganne naha
    anika even numbers >2 wil nt b processed
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    hmmmmm
    fine ur prog z correct ....

    sorry .......i dint get tht de value n .... actually by mistake t wz commented
    thts y t did an error

    bt still thr z n error

    t gives u 1 is a prime ...
    z t correct my dear ???????
    1 is nt a prime although ts facors r 1 n tself only

    n next error z u r investigating whether even numbers r primes xcluding 2 .......

    bt u r doing a very gud contribution 2 my C knowledge n my mates ....
    keep posting

    i did think that 1 is a prime when i was coding :P it should be it is dividable by 1 and itself which we see as a common thing in all other prime numbers, its just a definition not actual math. I didnt now the definition at the time of coding and i thought of the common factor in all primes and included 1 as a prime too
    The program is only made for integers not real numbers so excluding 2 and even 3 would not result in a wrong output because isprime is assigned true at the beginning and if some number was lesser than 2 then it would do nothing, the while would not execute and the answer will be delivered through a shortcut you can put i>3 also it wont result in wrong outputs it just uses the shortcut for those numbers that we have provided.
    If you are interested on programming, check out the calc i made its in my blog i made it with a friend of mine. :D think of the shortcuts before you start coding that might make ur prog a lot faster and that is what we all need.
    \/
    \/
    \/
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    hmmm
    gud i also like optimizing progz .....
    hv u hd a look on ma 1st prime investigating prog ...
    t z a marvellous optimization of prime investigation ....
    actually i cn do t verywell bt our newbies wil b confused looking thm @ once ...

    cn u pls let me know de link of ur calc .....

    mata ewanawada eke source code eka ....
    oya java dannawada ???
    mamath java walin calc ekak haduwa .....

    [email protected]
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    meke 1 prime ekak widiyata ganne naha
    anika even numbers >2 wil nt b processed
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        int isprime;
        cin>>n;
        isprime=true;
        i=abs(n); //neglect negative numbers
        n=i;
        while (i>2) {
              [B]if(n%2==0){isprime=-1;break;}[/B]
              i--;
              if (n%i==0) {isprime=false; break;} ;
              }         
        if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    sure i did check even numbers but dude y put the check inside the while loop ??? this will be slow as my original code :P
    put it like this to get it faster

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        int isprime;
        cin>>n;
        isprime=1;
        i=abs(n); //neglect negative numbers
        n=i;
        if (n%2==0 && n!=2) isprime=-1;
        while (i>2 && isprime==1) {
              i--;
              if (n%i==0) {isprime=0; break;} ;
              }         
        if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    marai ah eeee calc eka
    aiye mata eke source eka e-mail karannako
    eka VB walinda haduwe ??????

    yea fully VB, graph supports graphical differentiation and integration ;p mage pixu wada mona aiya da ane man 2008 AL mora ringa gana zscore madi UCSC yanawa hambunoth :P
    source eka denam commercial wada ba
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    please

    madurax86 said:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        int isprime;
        cin>>n;
        isprime=true;
        i=abs(n); //neglect negative numbers
        n=i;
        while (i>2) {
              [B]if(n%2==0){isprime=-1;break;}[/B]
              i--;
              if (n%i==0) {isprime=false; break;} ;
              }         
        if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    sure i did check even numbers but dude y put the check inside the while loop ??? this will be slow as my original code :P
    put it like this to get it faster

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int i,n;
        int isprime;
        cin>>n;
        isprime=1;
        i=abs(n); //neglect negative numbers
        n=i;
        if (n%2==0 && n!=2) isprime=-1;
        while (i>2 && isprime==1) {
              i--;
              if (n%i==0) {isprime=0; break;} ;
              }         
        if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;  
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }


    yes i agree
    mata arake source eka e-mail karannako