C Programmers Help Me!!!!

chamidilk

Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    kkkk
    t dznt matter f u dnt lik
    dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
    bt t z nt tht much eZ

    look @ dz

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

    hik hik de fastest
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    kkkk
    t dznt matter f u dnt lik
    dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
    bt t z nt tht much eZ

    look @ dz

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

    hik hik de fastest

    Shorter code doesnt mean its fast! you're still using the even check inside the while loop j doesnt change with i so why did you put it inside the while loop? theres no need of checking the same number whether its even or not again and again?, you make it faster by other things but make it alot slower by this
     

    x-pert

    Member
    Jun 13, 2006
    20,952
    77
    0
    chamidilk said:
    kkkk
    t dznt matter f u dnt lik
    dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
    bt t z nt tht much eZ

    look @ dz

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

    You're using a while loop. Hence slower in performance than the earlier one :D
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    madurax86 said:
    Shorter code doesnt mean its fast! you're still using the even check inside the while loop j doesnt change with i so why did you put it inside the while loop? theres no need of checking the same number whether its even or not again and again?, you make it faster by other things but make it alot slower by this

    i know t dear :P
    cn`t u understand y i say dz z de fastest ....
    c my prog calculates using only perfect squares upto de given number
    ur prog cn`t reach my prog`s speed ....
    i cn put de condition j%2 in an if block bt i dnt lik t ....
    there z a reason ...
    actually in ur prog also isprime is always checked until t bcomes -1 .....:P
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    x-pert said:
    You're using a while loop. Hence slower in performance than the earlier one :D

    i;ve told him twice to use the even check for j before the while but no point :P eya asa na lu but he says his prog is fast you know anyway that we can get tick count in c++?
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    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 && [B]isprime==1[/B]) {
              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;
    }


    wht do u say ????
    i know de point there

    i dnt say thr z no point

    :P :P :P :P :P :P :P
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    i know t dear :P
    cn`t u understand y i say dz z de fastest ....
    c my prog calculates using only perfect squares upto de given number
    ur prog cn`t reach my prog`s speed ....
    i cn put de condition j%2 in an if block bt i dnt lik t ....
    there z a reason ...
    actually in ur prog also isprime is always checked until t bcomes -1 .....:P

    i can understand that thats why i said u can make it faster by adding things but you let the even check be in the while loop is its slower than you think trust me i have been programming for 6 years :P
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    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 && [B]isprime==1[/B]) {
              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;
    }


    wht do u say ????
    i know de point there

    i dnt say thr z no point

    :P :P :P :P :P :P :P

    no you dont and you still dont get it, im talking about the part thats bold :P
    Code:
    #include<stdio.h>
    int main(){
        int i,j,s,ip;
        scanf("%d",&s);
        ip=1;
        j=s;
        i=2;
        while(s>2&&i*i<=s){
            if([B]j%2==0[/B]||s%i==0){ip=-1;break;}
            i++;        
        }    
        if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
        return 0;
    }
     

    chamidilk

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

    madurax86 said:
    no you dont and you still dont get it, im talking about the part thats bold :P
    Code:
    #include<stdio.h>
    int main(){
        int i,j,s,ip;
        scanf("%d",&s);
        ip=1;
        j=s;
        i=2;
        while(s>2&&i*i<=s){
            if([B]j%2==0[/B]||s%i==0){ip=-1;break;}
            i++;        
        }    
        if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
        return 0;
    }


    i got t dear bt tht code z urs ... nt mine
    checking isprime always n used break keyword 4 wht ?????
    check ur prog :P
    :P
    macho optimizing a prog z nt an eZ task
    api methana wada karana eke therumak naha
    mama post kale mage friendslata therenna ona widiyata
    dan mama dala thiyena ewawath oya dala thiyena ewawath ape kattiyata therenne naha ......
    mokada kattiya C walata aluth anika oya CPP

    oya cal ekak haduwa kiwwa neda ??????
    oyata eka hadanna dawas keeyak giyada ?????
    eken wadima hariya giye logic ekatada ???? optimization walatada ?????
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    i got t dear bt tht code z urs ... nt mine
    checking isprime always n used break keyword 4 wht ?????
    check ur prog :P
    :P
    macho optimizing a prog z nt an eZ task
    api methana wada karana eke therumak naha
    mama post kale mage friendslata therenna ona widiyata
    dan mama dala thiyena ewawath oya dala thiyena ewawath ape kattiyata therenne naha ......
    mokada kattiya C walata aluth anika oya CPP

    oya cal ekak haduwa kiwwa neda ??????
    oyata eka hadanna dawas keeyak giyada ?????
    eken wadima hariya giye logic ekatada ???? optimization walatada ?????

    this is my code
    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;
    }
    see i check for it on the beginning i may not try to optimize code be c++ is fast it self but i dont to things like that for sure ...dont edit my code inside quotes and post it looks like i actually posted it :P i didnt u edited it on your post above is my code if u want do a benchmark im sure that ur code isnt must faster than this because u still use the even check inside the while loop :P
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    hikz hikz karala balanna benchmark test eka mage code ekai oyage code ekai ...
    definitely u wil lose ...
    my code z fastest still u cnt understand wht my prog dz actually

    umbe code eke thiyena mage wagema aula thama mama pennuwe ....
     
    Last edited:

    NIPUNAYA

    Junior member
  • Jul 4, 2007
    599
    7
    18
    Kegalle
    senewimala1988 said:
    Here are two simple exercise on C programming language. Please give me the coding for these program(at least for the second exercise)(The second one is the most important exercise)

    Exercise 1:
    Write a program to input a temperature reading in either Celsius(c) or Fahrenheit(f) scale and convert it to the other scale. The temperature reading consist of a decimal integer followed by letter “F” or “f” if the temperature is in Fahrenheit scale or letter “C” or “c” if it is in Celsius scale. You may use a similar format for the output of the program.


    Note: c=5*(f-32)/9


    $ ./a.out
    Temperature reading: 100C
    212F


    $ ./a.out
    Temperature reading: 100F
    37C


    Exercise 2:
    Give a date as a triplet of numbers(y,m,d), with y indicating the year, m the month(m=1 for January, m=2 for February, etc.), and d the day of the month, the corresponding day of the week f(f=0 for Sunday, f=1 for Monday, etc.), can be found as follows:


    (a)if m<3 let m=m+12 and let y=y-1
    (b)let a=2m+6(m+1)/10
    (c)let b=y+y/4-y/100+y/400
    (d)let f1=d+a+b+1
    (e)let f=f1 mod 7
    (f)stop.


    Write a program that will read a date and print the corresponding day of the week. All divisions indicated above are integer divisions.


    :(:(:(

    Saahena bonda oone equation ekak wagei penne :/
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    chamidilk said:
    hikz hikz karala balanna benchmark test eka mage code ekai oyage code ekai ...
    definitely u wil lose ...
    my code z fastest still u cnt understand wht my prog dz actually

    haha due u r funny do u think others are dumb brutes ? very funny if u insist
    what you are doing is checking only until the biggest number that can divide a number with 0 as remainder the biggest number that can do is, is the square root of the number so what is there to think so much? theres no point in dividing by numbers higher than the square root of the number, why did i not use it? because i didnt want to optimize the code unlike you boasting about how fast your code works but still u dont understand that you're code gets slower much more because you are using j%2 check inside the while loop ...therene natam man mona karanada :P
     

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    matath eka thama kiyanna thiyenne hallo

    mama code eke athule j%2 check karanne
    oya methana while loop eka athulema isprime chcek karanawa anika break keyword ekath pawichchi karal mokatada break ain karanna


    u r de funny one who think i am a blind man

    oya 29 prime kiyanna 28 idan 2 wenakan enawa
    honadata balanna mage prog eka mama yanne 2 idan 6 wenakan witharai hello

    u cnt challenge me

    i cnt explian u further more u cnt understand i think .....
    t dznt matter dnt play fool with me
     
    Last edited:

    chamidilk

    Active member
  • Nov 18, 2006
    1,053
    4
    38
    Moratuwa
    madurax86 said:
    pandithayoth eka katha karala wadak na


    siera macho
    hik hik umba danan idala thiyenawa neda shuk ......marai ah ...y dnt u use t
    eeta passe umba ahanawa mama ai if(j%2==0&&...) check karanne ai kiyala
    anunta upades didi panditha kan karana ewunta mageth wadi kamaththak naha ....
    mata umba kiyana ka therenewa.
    but umbata naha ...........
     
    Last edited:

    senewimala1988

    Well-known member
  • Dec 5, 2007
    7,745
    2,593
    113
    Kadawatha, Sri Lanka
    chamidilk said:
    menna temperature conversion prog eka

    #include<stdio.h>
    main(){
    int c;
    int f;
    char type;
    printf("Temperature reading: ");
    scanf("%d%c",&c,&type);
    if((type=='c'||type=='C')){
    printf("%dF",F(c));
    }
    else if((type=='f'||type=='F')){
    printf("%dC",C(c));
    }
    else printf("Invalid Entry\nTry Again");
    }

    int C(int i){
    return (5*(i-32)/9);
    }
    int F(int i){
    return (32+9*i/5);

    }
    Macho oya red coloured set eka explain karapanko...Mama oya out put ekama enna program eka modify kala.Eth oyage oya red coloured coding eka therenne ne...
     

    Im.Just.a.Fool

    Junior member
  • Apr 2, 2009
    202
    19
    18
    කියන්නෙම නෑ
    chamidilk

    oya prime numbers check karana kramaya hari. n is a prime if it is not divisible by x (2 <= x <= sqrt(n) ) . ea range eke numbers walin n bedenawada kiyala check karana kramaya thamai fast ma method eka. iratte sankya check kale nathnam program eka thawath fast wenawa. Because there is a 50% chance that any given integer is divisible by 2.

    Code:
        [COLOR=Green]// This method assumes n is a non-negative integer, because 
        // prime numbers start with 2.[/COLOR]
        boolean isPrime(int n) {
            if (n < 2) return false;   
            if (n == 2) return true;
            if (n % 2 == 0) return false;
            for (int i = 3; i*i <= n; i+=2)
                if (n % i == 0) return false;
            return true;
        }
     

    Im.Just.a.Fool

    Junior member
  • Apr 2, 2009
    202
    19
    18
    කියන්නෙම නෑ
    chamidilk said:
    Code:
    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");
        }
    }

    You can write the same program using two for loops like this (in Java):

    Code:
            for (int i = 0; i < 11; i++) {
                for (int j = 1; j <= 6-Math.min(i, 10-i); j++)
                    System.out.print("$");
                System.out.println();
            }
    System.out.print("$") = printf("$") in C
    System.out.println(); = printf("\n");
    Math.min(x,y) walin karanne x saha y walin kuda sankyawa hoyana eka. C/C++ walath meeta samana function ekak athi.