Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array length of a long value
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: Thu Nov 03, 2005 5:48 pm   Post subject: (No subject)

What makes you think he was talking about C++? Maybe he was, but I'm sure you could make some even faster optimised Assembly code. Try to keep your mind open on things like this.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Thu Nov 03, 2005 5:57 pm   Post subject: (No subject)

it's simple actually... here's how you'd implement it..

#1: use a multi-dimensional rectangular array (let's take a 2D array, as a 2D array will provide for storing a maximum of (2^32 - 1)*(2^32 - 1) values... *a huge number!!*
#2: fix the ROW and COL dimension to MAX_INT
#3: create a class.. that will handle the 1D -> 2D conversion for you.


now i'm not going to create this class for you but... here's the basic mechanics behind such a class:
code:
long[][]  primes = new long[Integer.MAX_INT][Integer.MAX_INT];


basically the Nth value in the emulated 1D-array is the (row*Integer.MAX_INT+col)th value in the 2D "primes" array. so in ur long get(long n); you should basically have
code:
**refer to beard0's post**
(edit: sorry dude didn't see ur post)

note: the code is not tested... it could be wrong.. but hopefully you get the idea Wink
also note: long may not be a large type to encompass all those values.. -,-'' You might have to use the BigDecimal class.. Or even better reduce the size of the 2D "primes" array.
Naveg




PostPosted: Thu Nov 03, 2005 6:10 pm   Post subject: (No subject)

Well the total size of the array only needs to be a number specified by the user. As well, the array type can be boolean (ie. is the index of this value prime of not).
rizzix




PostPosted: Thu Nov 03, 2005 6:12 pm   Post subject: (No subject)

ah well all the better then.. but you get the idea..
beard0




PostPosted: Fri Nov 04, 2005 3:44 pm   Post subject: (No subject)

Err... did nobody read my post about 2D arrays on the previous page?
wtd




PostPosted: Fri Nov 04, 2005 3:50 pm   Post subject: (No subject)

Hey, you know what might work? Using a two dimensional array.

Wink
rizzix




PostPosted: Fri Nov 04, 2005 4:42 pm   Post subject: (No subject)

there's a way to do it with a 2D array?? cool!! how? Razz
Naveg




PostPosted: Fri Nov 04, 2005 4:43 pm   Post subject: (No subject)

would it though? In theory it would yes, but I'm still restricted by java's memory limitations...which are currently reached even before an array of MAX_INT values.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Fri Nov 04, 2005 8:41 pm   Post subject: (No subject)

ok here's my shot at a solution...
Java:

import java.util.*;
import java.math.*;

class test {
    final static BigInteger ONE = BigInteger.ONE;
    final static BigInteger ZERO = BigInteger.ZERO;
   
    public static void main(String[] args) {
        for (BigInteger i : primesTakeN(new BigInteger("10000")))
            System.out.println(i);
    }

    public static List<BigInteger> primesTakeN(BigInteger numPrimes) {
       
        List<BigInteger> primes = new LinkedList<BigInteger>();
        List<BigInteger> indices = new LinkedList<BigInteger>();
       
        for (BigInteger i = new BigInteger("2");
             i.compareTo(numPrimes) < 0; i = i.add(ONE)) {
            primes.add(i);
            indices.add(i);
        }
       
        Iterator<BigInteger> it = indices.iterator();
        while (it.hasNext()) {
            BigInteger m = it.next();
            Iterator<BigInteger> it2 = it;
            while(it2.hasNext()) {
                BigInteger n = it2.next();
                if (n.mod(m).equals(ZERO))
                    primes.remove(n);
            }
        }
       
        return primes;
    }
}
Notice i've used the BigInteger class... So it should work with arbitrary long numbers... either way.. this is not the "fastest" solution (especially cuz of the BigInteger class)... but.... gah!
HyperFlexed




PostPosted: Sat Nov 05, 2005 6:23 pm   Post subject: (No subject)

I've never seen the purpose of computing primes. I sure hope I never get the project.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 25 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: