The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Euler's_totient_function. in C++
Discuss Euler's_totient_function. in C++ in the C Programming forum on Dev Shed. Euler's_totient_function. in C++ C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 25th, 2013, 09:52 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 36 m 6 sec
Reputation 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();
}
|

February 25th, 2013, 10:40 AM
|
 |
Contributed User
|
|
|
|
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.
|

February 25th, 2013, 10:46 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 2
Time spent in forums: 36 m 6 sec
Reputation Power: 0
|
|
|
so how exactly i correct the programee ????
|

February 25th, 2013, 11:51 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

February 25th, 2013, 12:08 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by DeepChanda486 so how exactly i correct the programee ???? |
Quote: | 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.
|

February 25th, 2013, 12:41 PM
|
 |
Contributed User
|
|
|
|
|
> 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|