#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Euler's_totient_function. in C++


    i am working on a programme to evaluate Euler's_totient_function.
    the code fragment doesnt show any error yet returns the same value with no change..helP...
    Code:
    #include<iostream.h>
    #include<conio.h>
    int check(int a)
    {
    int i,j,flag=0;
    for(i=2;i<=a/2;i++)
    {
    j=a%i;
    if(j==0)
    flag++;
    }
    if(flag==0)
    return 1;
    else
    return 0;
    }
    void main()
    {
    int i,num,p;float s,k,t=1.0;
    cout<<"\n Enter the number";
    cin>>num;
    p=num;
    for(i=2;i<num;i++)
    {
    if(num%i==0)
    {
    if(check(i)==1)
    {
    k=1-(1/i);
    t=t*k;
    cout<<"\n "<<k;

    }
    }
    }
    cout<<"\n "<<p*t;

    getch();
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Step 1 is learn about [code][/code] tags, then your code might look presentable.
    Code:
    #include<iostream.h>
    #include<conio.h>
    int check(int a)
    {
      int i, j, flag = 0;
      for (i = 2; i <= a / 2; i++) {
        j = a % i;
        if (j == 0)
          flag++;
      }
      if (flag == 0)
        return 1;
      else
        return 0;
    }
    
    void main()
    {
      int i, num, p;
      float s, k, t = 1.0;
      cout << "\n Enter the number";
      cin >> num;
      p = num;
      for (i = 2; i < num; i++) {
        if (num % i == 0) {
          if (check(i) == 1) {
            k = 1 - (1 / i);
            t = t * k;
            cout << "\n " << k;
          }
        }
      }
      cout << "\n " << p * t;
    
      getch();
    }
    > k = 1 - (1 / i);
    Now look up integer arithmetic.
    k might be a float, but the conversion from int to float happens a lot later than you think it does.

    > #include<iostream.h>
    > #include<conio.h>
    It's been about 15 years since iostream.h was a valid way of including C++ IO streams, and 20+ years since conio.h last had some meaningful relevance.

    In other words, your compiler is beyond obsolete (in the same way that Egyptian hieroglyphics can be considered a modern writing system).

    Not to mention the whole void main thing being wrong as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    so how exactly i correct the programee ????
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Re-write it from scratch using programming techniques from the last couple decades. Your program is so old nobody remembers how to fix it. If you're doing this for homework, drop the course and buy a C++ book written after 2005 on amazon. If you're doing this for work, buy a C++ book written after 2005 on amazon and talk to your boss about modernizing your codebase.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Originally Posted by DeepChanda486
    so how exactly i correct the programee ????
    Originally Posted by salem
    > k = 1 - (1 / i);
    Now look up integer arithmetic.
    k might be a float, but the conversion from int to float happens a lot later than you think it does.
    1/1 = 1
    1/2 = 0
    1/3 = 0
    1/4 = 0
    1/5 = 0
    1/6 = 0
    1/7 = 0
    1/8 = 0
    1/9 = 0
    1/10 = 0
    1/11 = 0
    1/12 = 0
    1/13 = 0
    1/14 = 0
    1/15 = 0

    Are you beginning to see a pattern with integer division?

    To get floating-point division, at least one of the operands, dividend or divisor, needs to be a floating-point datatype.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > Today, 04:40 PM salem wrote many words of wisdom.

    > Today 04:46 PM DeepChanda486 wrote
    > so how exactly i correct the programee ????

    My next advice is spend more than 6 minutes learning from what was said before bouncing right back with another "feed me" post.

    Allowing for propagation delays, the time it takes the board to send an email, the time it takes you to receive that email, read it, do something, come back to the board etc etc, one wonders whether you actually even read my post at all, save to determine that it didn't contain your code with all the corrections already done.

    YOU fix it, then you'll understand both
    a) how to avoid finding yourself at the bottom of this hole again.
    b) if you happen to fall into the hole again, you'll know the symptoms to look for and how to solve the problem.

    Comments on this post

    • ManiacDan agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo