Community

Do you have what it takes to be the next brilliant trader, researcher, or developer at Jane Street Capital? Find out in the Careers in Mathematics Forum.
Login Register Memberlist Search AoPS Blogs Contests Galleries Forum Index
The time now is Wed Dec 02, 2009 4:44 am
All times are UTC - 8
View posts since last visit
View unanswered posts
number of irreductible fractions
Moderators: High School Olympiad Moderators, amfulger, Arne, darij grinberg, freemind, harazi, Megus, N.T.TUAN, orl, pbornsztein, ZetaX
Post new topic   Reply to topic View previous topicView next topic
16 Posts • Page 1 of 1
Author Message
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#1
number of irreductible fractions

so,i got this informatics problem actually.
i want to reformulate in math therms

it's like,i have this 1 \leq N \leq 10^6 and
i want to find the number of all fractions \frac{p}{q} with 1 \leq p,q \leq N
so that these fractions are irreductible,this meaning that there isn't any
k \in [2,min(p,q)] so that k|p and k|q (ofcourse k,p,q,N \in N)

so,for N=4 we have the following fractions that satisfy our
conditions:
\frac{1}{1},\frac{1}{2},\frac{1}{3},\frac{1}{4},\frac{2}{1},\frac{2}{3},\frac{3}{1},\frac{3}{2},
\frac{3}{4},\frac{4}{1},\fra...

i probably will have to go about an approach with \phi(n) but remember that it will have to use little operations to be a fast method, a fast algorithm actually.
i thank anyone who can help me in advance

PostPosted: Mon Mar 14, 2005 8:55 am  Back to top 
  ProfilePMYMBlog
Myth
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 02 Sep 2003
Posts: 4490
Location: Chelyabinsk, Russia
Russian Federation

To rate posts you must be logged in
#2
I have some doubts there is a fast algorithm to find \sum_{k=1}^N\varphi(k). Confused
_________________
Myth is out of here

PostPosted: Mon Mar 14, 2005 9:34 am  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#3
about the algorithm

i really remember that i saw in a book a formula
for \sum_{k=1}^{N} (\phi(k))

in the mean-time look at what i've done to compute it into a c++ program


#include <stdio.h>
#include <math.h>
long N,i,j,k,nr;
char E[1000000]; //here i mark the numbers
//this is eratosthenes
//sieve
long phi[1000000]; //here phi[i] is generated
int main(){
FILE *f=fopen("fractii.in","r");fscanf(f,"%d",&N);//takes N from file
long nr=0;
phi[1]=0;
phi[2]=1;
phi[3]=2;
phi[4]=2;
phi[5]=4;
phi[6]=1;
phi[7]=6;
phi[8]=4;
phi[9]=6;
phi[10]=4;//these are precalculated values
for(i=2;i<sqrt(N)+1;i++)
{
long t=i;
while(t<=N)
E[t]=1,t+=i;
}//here i've marked the multiples
//of i and the j's with E[j]=0
//that remain are prime
//according to eratosthenes
for(i=11;i<sqrt(N)+1;i++)
{
if(E[i]==0)
{
long t=i;
if(E[i]==0)phi[i]=i-1;//if i is prime i know
else //that E[i]=0
while(t<=N) //i also know that
phi[t]*=(i-1)/i,t+=i;//for a prime i
} //,phi[i]=i-1;
}// here i calculate phi[t] step by step
// as i see every prime number by the formula
// phi(n)=n*(1-1/p1)*(1-1/p2)*...*(1-1/pn)
// primes generated through the sieve
for(i=1;i<=N;i++)
nr+=2*phi[i];
//now i multiply phi[i] by two
//because for example if i have
//ireductible fraction 2/3
//i also have ireductible fraction
//3/2 so i have to multiply by two this number
FILE *fo=fopen("fractii.out","w+");
fprintf(fo,"%d",nr);
return 0;
}

PostPosted: Mon Mar 14, 2005 9:49 am  Back to top 
  ProfilePMYMBlog
Myth
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 02 Sep 2003
Posts: 4490
Location: Chelyabinsk, Russia
Russian Federation

To rate posts you must be logged in
#4
Are you participating in a programming contest?

BTW, there is no any appropriate formulae for \sum_{k=1}^N\varphi(k).
_________________
Myth is out of here

PostPosted: Mon Mar 14, 2005 9:58 am  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#5
algorithm

tell me what do you think about the algorithm.
try it(compile+run).

PostPosted: Mon Mar 14, 2005 10:04 am  Back to top 
  ProfilePMYMBlog
Myth
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 02 Sep 2003
Posts: 4490
Location: Chelyabinsk, Russia
Russian Federation

To rate posts you must be logged in
#6
I am sure you are able to compile it yourself Razz

Some aspects of your algorithm are not clear for me. Confused
_________________
Myth is out of here

PostPosted: Mon Mar 14, 2005 10:12 am  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#7
after i've done erathosthenes for the interval [1,N]
i have


E[i]=
/ 0,if i is prime
|
\1,if its composite

now, i know the prime numbers between 1 and N.

so now i need to calculate phi[i] for all i\in [1,N]
for the sum i am searching for is really what you
stated before:

\sum_{k=1}^{n} (phi(k))

then i calculate phi(k) for each k and add up to s

PostPosted: Mon Mar 14, 2005 10:24 am  Back to top 
  ProfilePMYMBlog
jmerry
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 12 Jun 2004
Posts: 7514
Location: Seattle
United States

To rate posts you must be logged in
#8
We're probably more interested in your algorithm than in the exact details of how it is written.

Your algorithm: start by finding all primes less than N using the sieve of Eratosthenes. This takes O\left(\sum_{\substack{p\text {is prime}\\ p<N}}\frac Np\right)=O(N\log\log N) computations, if done right.

During this pass through the array, we build an array of values of \phi. I'm not quite sure if what you're doing makes sense here. I would do it as:
(initialize phi(k)=k, notprime(k)=0,sum=1)
(let i be an index variable, starting at 2 and increasing to N)
.....(if notprime(i)=0, execute following; else do nothing)
..........(let j=i)
..........(multiply phi(j) by (i-1)/i)
...............(increase j by i, exit if j>N)
...............(set notprime(j)=1)
...............(multiply phi(j) by (i-1)/i)
.....(increase sum by 2*phi(i))

This will produce correct values of \phi(k) and \sum_k 2\phi(k), in O(N\log\log N) time. The only drawback is the large amount of storage space required. As far as I know, there's no faster way, and N\log\log N is very fast. In particular, the sieve is the fastest way to find all of the primes between 1 and N.

The nice formula is that \sum_{d|n}\phi(d)=n. That doesn't help here.
If you want an algorithm that doesn't use the storage space, you'll have to settle for much slower. Using the Euclidean algorithm to determine whether a pair is relatively prime gets you O(N^2\log N); using factoring algorithms to find \phi(k) without the sieve gets you something like O(N^{3/2}).

By the way, \phi(1)=1. It's a special case anyway; we add 1 instead of 2 in our sum. Also, \phi(6)=2. I don't see why you need precalculated values; your algorithm should be able to calculate them, except for the special case \phi(1).
How close is the sum you obtain to \frac{6N^2}{\pi^2}?


To anyone: Is there any way to format spacing in "normal" mode? That pseudocode needs indents, and I can't do them with spaces.

PostPosted: Mon Mar 14, 2005 9:54 pm  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#9
solved

i've done it afterall.
it takes 40% of tests but i'm staisfied
it's now mathematically correct.
thanks all

#include <stdio.h>
//#include <math.h>
//#include <time.h>
#define mare unsigned long long
mare N,i,j,k,nr;
int E[1000000]; //here i mark the numbers
//this is eratosthenes
//sieve
mare phi[1000000]; //here phi[i] is generated
int main(){
//-----
//clock_t start=clock();
//-----
FILE *f=fopen("fractii.in","r");fscanf(f,"%d",&N);//takes N from file
mare nr=0;

for(i=2;i<=N/2;i++)
{
if(E[i]==0)
for(j=2*i;j<=N;j+=i)
E[j]=1;//ciur
};
for(i=1;i<=N;i++)if(E[i]==1)phi[i]=i;

for(i=2;i<=N;i++)
{
if(E[i]==0)
{
phi[i]=i-1;
for(j=2*i;j<=N;j+=i)
phi[j]/=i,phi[j]*=(i-1);
};
};
//phi[1..N];
//phi[i]*2=nr fractii ireductibile a/b | 1<=a,b<=i
for(i=2;i<=N;i++)
nr+=2*phi[i];
nr++;
FILE *fo=fopen("fractii.out","w+");
fprintf(fo,"%u",nr);
//-----------------
//clock_t end=clock();
//fprintf(fo,"\n%f",(double)(end-start)/CLOCKS_PER_SEC);
//-----------------
return 0;
}

PostPosted: Mon Mar 21, 2005 7:13 am  Back to top 
  ProfilePMYMBlog
jmerry
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 12 Jun 2004
Posts: 7514
Location: Seattle
United States

To rate posts you must be logged in
#10
It's not mathematically correct- you're missing \frac11.

Also, you could combine the loops as I wrote to slightly increase efficiency.

PostPosted: Mon Mar 21, 2005 9:22 am  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#11
about these fractions

i am counting \frac{1}{1} because of that last nr++ if you look better.
it's at the end of the code

jmerry you are smart.
its verry interesting what you say there in the last part
of your post,it particularly screamed at me.
that that sum i'm searching for is almost
\frac{6N^2}{(\pi)^2}
i remember i saw that somwhere....where have i saw
it ...oh oh yes ....it had sumthin to do with partial
sums of phi function.
i just skimmed it a little bit and that approx.
remained in my head...
but still...the problem asks for concrete value,
wich brings me to the question is there any posibility
of going around that value until we get to the right one.
i mean can we make a set of steps to get from the
approx. to the concrete value?

i'll send attached a pdf on my hdd where i saw them
before
man...how would i want to have the time to read that stuff
i'm still in highschool,i would have to attend college
but at the rythme i'm studying at my maternal language
and physics it seems to me ill never get to make somethin
out of my life.
anyway i would want to read this stuff but i'm in way
over my head with school...i barely have time to post
but now i'm in some hollydays...yeaaaaaaaaaa!!!
i am indeed verry stupid

PostPosted: Tue Mar 22, 2005 10:06 am  Back to top 
  ProfilePMYMBlog
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#12
where i saw the approximation

this is where i saw the approx
10.zip
Description 
zip

 Download 
Filename  10.zip 
Filesize  48.1KB 
Downloaded  72 Time(s) 

PostPosted: Tue Mar 22, 2005 10:13 am  Back to top 
  ProfilePMYMBlog
jmerry
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 12 Jun 2004
Posts: 7514
Location: Seattle
United States

To rate posts you must be logged in
#13
spx2 wrote:
i mean can we make a set of steps to get from the
approx. to the concrete value?

No. I can show that the error is O(N\log N) (and probably smaller) with constant no worse than 2. Conversely, the error must frequently be comparable to N or larger. The exact value is certainly very irregular.
Getting at these errors exactly would require computing \mu(k) for k<N, which is at least as much work as computing \phi(k).

Your method does look like a very good algorithm, and I don't think there's any way to do it significantly faster. Its only drawback is the quantity of storage required.

PostPosted: Tue Mar 22, 2005 3:47 pm  Back to top 
  ProfilePM
fedja
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer

Offline
Joined: 04 Feb 2005
Posts: 4279
Russian FederationUnited States

To rate posts you must be logged in
#14
jmerry wrote:
As far as I know, there's no faster way, and N\log\log N is very fast.

If your task is just to find the number of relatively prime pairs for one particular N, and not for all values of N up to a certain one, which is what jmerry's algorithm really does, the running time can be reduced to N^{2/3}\log\log N. The trick is to use the simple identity
\Phi(N)=N^2-\Phi([N/2])-\Phi([N/3])-\ldots-\Phi([N/N])
where \Phi(N) is the number of irreducible pairs and [x] is the integer part of x.
This allows you to figure out \Phi(N) if \Phi([N/2]), \Phi([N/3]) etc. are known in time \sqrt N (Think why it is not N Wink! Hint: it is not very clever to sum the terms \Phi([N/k]) with k>\sqrt{N} one by one...). Now run jmerry's algorithm to figure out \Phi(n) for n\leq N^{2/3} and then use the above trick to find \Phi([N/k]) with k<N^{1/3} (you will need only these values because [[N/k]/\ell]=[N/(kl)].
I think this algorithm can be improved too. Its advantages are both time and space reduction by factor of N^{1/3}, which for N=10^6 may be already noticable Razz

PostPosted: Tue Mar 22, 2005 5:24 pm  Back to top 
  ProfilePM
jmerry
Birch & Swinnerton Dyer
Birch & Swinnerton Dyer


Offline
Joined: 12 Jun 2004
Posts: 7514
Location: Seattle
United States

To rate posts you must be logged in
#15
Any further improvement on fedja's algorithm is minor- you actually only need the values for k square-free, but this is asymptotically a constant fraction.

In the following, {a/b} denotes integer division.
Here's how I would do it. Start by choosing M=A^3 (slightly) greater than N.
(Create arrays notprime[n], phi[n], sumphi[n] of size A^2)
(Create array mu[n] of size A-1)
(Initialize notprime[n]=0, phi[n]=n)
(Initialize mu[n]=0)
(Let S=0)
(set sumphi[1]=1)
(let i be an index variable, starting at 2 and increasing to A^2)
.....(if notprime[i]=0, execute following; else do nothing)
..........(let j=i)
..........(multiply phi[j] by (i-1)/i)
...............(increase j by i, exit if j>N)
...............(set notprime[j]=1)
...............(multiply phi[j] by (i-1)/i)
.....(set sumphi[i]=sumphi[i-1]+2*phi[i])
(set mu[1]=1)
(let i be an index variable, starting at 1 and increasing to A-1)
.....(if mu[i]=0 do nothing; else execute following)
..........(Increase S by {N/i}^2*mu[i])
..........(Decrease S by mu[i]*sumphi[1]*({N/i}-{N/2i}))
..........(let j be an index variable, starting at 2 and increasing until j^2*i>N)
...............(if i*j < A decrease mu[i*j] by mu[i])
...............(else decrease S by mu[i]*sumphi[{N/ij}])
...............(Decrease S by mu[i]*sumphi[j]*({N/ij}-{N/i(j+1)}
(Output S)

This is very hard to understand, but it uses far less space and time. The first part takes A^2\log\log A time, and the second takes \frac{N}{\sqrt{A}} time, for overall N^{2/3}\log\log N time. Alsom the storage is only N^{2/3}.

We could calculate \mu somewhat more efficiently, but it doesn't really matter here.

PostPosted: Wed Mar 23, 2005 12:14 am  Back to top 
  ProfilePM
spx2
Yang-Mills Theory
Yang-Mills Theory


Offline
Joined: 07 Feb 2005
Posts: 646
Romania

To rate posts you must be logged in
#16
Thanks gang

thank you all for remarks and help

PostPosted: Tue Mar 29, 2005 1:37 am  Back to top 
  ProfilePMYMBlog
Display posts from previous:   Sort by:   
16 Posts • Page 1 of 1
Post new topic   Reply to topic View previous topicView next topic
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
You cannot post calendar events in this forum


© Copyright 2008 AoPS Incorporated. All Rights Reserved. • FoundationPrivacyContact Us