List of numbers

Analysis of algorithms, Algorithms, Data structures. Get help with graph algorithms, search algorithms, string algorithms, sorting algorithms, merge algorithms, compression algorithms, optimization and quantum algorithms on My Computer Forum

Moderators: shynthriir, SidT

List of numbers

Postby Quantum++ » Fri Nov 26, 2010 5:34 pm

How can I find/program a list of:
We only operate with natural numbers.

1. The prime numbers < 10 000
2. x = a^2 + b^2 < 10 000
3. x = a^3 + b^3 < 10 000
4. x = a^2 + b^2 + c^2 < 10 000
5. x = a^3 + b^3 + c^3 < 10 000

Thank you.
Quantum++
 
Posts: 1
Joined: Fri Nov 26, 2010 5:33 pm

Re: List of numbers

Postby julien » Sat Dec 04, 2010 2:48 am

You could write such a program easily:
1. Use Eratosthene's sieve
2 and following: bound the values of a, b, c (obvious bounds) and test for all the possibilities within your boundaries
User avatar
julien
Administrator
 
Posts: 413
Joined: Sun Dec 02, 2007 4:12 am

Re: List of numbers

Postby Xitami » Wed Feb 16, 2011 12:09 pm

Code: Select all
void q2(int x){
   int a,b;
   for(a=1;a<=71;a++)    //2*71^2 <= 10'000
      for(b=a;b<=71;b++)
         if (a*a+b*b==x)
            printf("%d %d\n",a,b);
}
Xitami
 
Posts: 3
Joined: Wed Feb 16, 2011 11:43 am

Re: List of numbers

Postby Xitami » Thu May 17, 2012 12:54 pm

Code: Select all
#define N 10000
main() {
        int i, j, t[N]={0,0,1};
        for( i=3; i<N; i++ )
                t[i]=2;
        for( i=2; i<N; i++ )
                for( j=i+1; j<N; j++ )
                        t[j] = (t[j]*i) % j;
        for( i=0; i<N; i++ )
                if(t[i])
                        printf("%d ", i); }
Xitami
 
Posts: 3
Joined: Wed Feb 16, 2011 11:43 am

Re: List of numbers

Postby julien » Wed May 23, 2012 11:09 am

This algorithm was improved over the years ;)
User avatar
julien
Administrator
 
Posts: 413
Joined: Sun Dec 02, 2007 4:12 am


Return to Algorithms and Data Structures

Who is online

Registered users: Bing [Bot], Google [Bot]