Array size limitations... and prime number programs.

What are they? I am an ameteur programmer who wrote an extremely efficient prime finding program, if i do say so myself. The program doesnt find high primes, just prime numbers from 1-1,000,000,000 so far. Thats one version, the next version i use is faster, but because it uses an ArrayList, it can only do the primes 1-10,000,000. I am trying to work the programs up to primes with 50 or more digits, but ArrayList runs out of space, Integer is too small, I dont quite know how to switch all my code to be compatable with BigIntegers, and I cannot find a limit to arrays. I guess that I could find big primes if a) Integer was bigger, b)I tried, but what i wanted to do, because the second program is way more effecient, is to use an array to store primes instead of an ArrayList. The only problem? Arrays cannot be appended, so I need to know the limit in size for an array... So far, from my tests, I have found the limit to be somewhere around, 15,380,277. The only problem with this is that every time i compile, i can use a different number. So I would like it if a) somone could tell me the limit to an array's size, b) ideas for programs that can find primes with 50 or more digits, c) Down below is the code, could someone tell me how to convert it to BigIntegers?
  private void primeFinder1root(int beg, int end){
    int tmp = 0;
    int counter = 0;
    int counter2 = 2;
    boolean flag;
    for(int n = 3; n < end; n+=2){
      if(n%5!=0){
        flag = true;
        for(int d = 3; d <= Math.sqrt(n) && flag; d+=2){
          counter++;
          if(n%d == 0)
            flag = false;
        if(flag){
          System.out.println(n);
          counter2++;
          tmp = n;
          if(counter2%100000 == 0)
            System.out.println(n);
    System.out.println();
    System.out.println("The program looped " + counter + " times. There were " + counter2 + " primes found. "
                         + tmp + " was the last prime found.");
  }That is the first progam that does not use an ArrayList, but is still extremely effecient, it only looped 1,744,118,556 times to find the primes 1-100,000,000 which seems like a lot, but it truely isn't. I realize that by using counters and printing, I am slowing the program down immensly, but i am fine with that.
  public void primeFinder(){
    boolean flag;
    int tmp = 0;
    int tmp2 = 0;
    int counter = 0;
    primes.add(2);
    for(int n = 3; n < end; n+=2){
      if(n%5!=0){
        flag = true;
        for(int i = 0; i < primes.size()/2 && ((Integer)primes.get(i)).intValue() <= Math.sqrt(n) && flag; i++){
          tmp = ((Integer)primes.get(i)).intValue();
          if(n%tmp == 0)
            flag = false;
          counter ++;
        if(flag && n!=1){
          System.out.println(n);
          primes.add(n);
          tmp2 = n;
          if(primes.size() % 100000 == 0)
            System.out.println(n);
    primes.add(0, 1);
    System.out.println(counter + " " + primes.size() + " " + tmp2);
  }This is the code that stores all the primes it finds in an ArrayList, and then compares all numbers to the ArrayList of primes. This is extremely more effecient than the first, looping only 278,097, 308 times to find the primes 1-10,000,000 (the other looped 868,772,491 times). I used 10,000,000 as my example because this program cannot go to 100,000,000 because there are 5,761,455 primes and the ArrayList can only hold ~4,000,000 objects. Because of this ~4,000,000 object limitation on the ArrayList I have set my sites on the Array. This is the reason why I want to know the limitations of an Array if you could please tell me. If you could also, I would like help making my code compatable with BigIntegers as well.
Well, sorry for the very long post, but thank you if you answer it and took the time to read it.
Dumber_Child

I too was in the quest to develop the most efficient prime number code few years ago when I was a student.
Here is a more fine tuned version for your code
   public static long findPrimes(int max, ArrayList out){
      long count=0;
      FastList primes = new FastList((int)Math.sqrt(max));
      primes.add(2);
      for (int i=3; i<=max; i+=2)
         int al_size = primes.size();
         double sqrt = Math.sqrt(i);
         boolean prime_flag =true;
         boolean loop_flag = prime_flag;
         for (int j=0; j<al_size && loop_flag; j++)
            int val = primes.get(j);
            if (i%val == 0)
               loop_flag = prime_flag = false;
            else if (val > sqrt)
               loop_flag = false;
            count++;
         if (prime_flag)
            primes.add(i);
      primes.addToArrayList(out);
      return count;
Following a data structure to store the prime numbers while processing
Since this holds first number of primes in an array instead of in an ArrayList
the get will work much faster and for those elements the casting is not required
   static class FastList
      ArrayList list = new ArrayList();
      int cache[];
      int pointer = 0;
      int fastAreaSize;
      public FastList(int fastAreaSize){
         cache = new int[fastAreaSize];
         this.fastAreaSize = fastAreaSize;
      public void add(int i){
         if (pointer < fastAreaSize)
            cache[pointer] = i;
         list.add(new Integer(i));
         pointer++;
      public int size(){
         return pointer;
      public int get(int i){
         if (i<fastAreaSize)
            return cache;
else
return((Integer)list.get(i)).intValue();
public void addToArrayList(ArrayList al){
for (int i=0; i<pointer; i++)
if (i<fastAreaSize)
al.add(new Integer(cache[i]));
else
al.add(list.get(i));
When running in my pc
above code detected primes within range 0-10000000 in 281809517 iterations within 6.718secs
while your original code did the same in 278097308 iterations within 13.687 secs.
By the way i removed the check for '5' thats why my code does more iterations.
Notice that I have relocated code like Math.sqrt and ((Integer).....).intValue() to reduce the re calculating the same thing. That saved a lot of time.

Similar Messages

  • Mailbox and Library Size Limits and Reporting

    I have seen quite a few postings on size limits.
    I have a system that has two Post Offices - one for mail and one for doc mgt. Users log into the e-mail PO and get redirected to the doc mgt PO when they need to access files in the libraries ( there are 6 libraries)
    I would like to find a utility other than GW Check - maybe a nice Windows GUI app that lists the amount of space each user is using in each PO (or total) that would include all e-mails, sent and received (with attachments) and documents in the libraries.
    I would also like to apply space limits to each user. "Client Options" sets it globally, but for each DOM or PO separately, and of course, you can be more granular on a per user basis. Setting the limit on a per user basis is daunting since it has to be done one at a time!
    Is there a utility that not only reports consumed space, but also allows you to set the limits?
    BTW, why are the disk space limits under the "Send" section of Client Options. Seems like it would be more of an "Environment" option.
    Using GW 7.02HP on NW 6.5.6
    Many thanks,
    Charlie Riale, B.E.E.,CNE6
    CARiale at bnetinc.com
    www bnetinc com
    610-645-7616 (Work)
    610-645-7617 (Fax)

    I'm not sure of any utilities that do a "report and set" of the limits. The GWCheck Log File Parser that Dave Parkes wrote (Caledonia Network Consulting) works well for seeing how much space a user has in use. That said, you CANNOT set limits on users in the DMS - just isn't possible. It's a different system, and does not allow for size limits of documents, etc.
    As for why size limits is located under "sent" I guess it's because it really affects the sending of mail the most. I.e., when a user reaches the size limit, incoming mail is not bounced. The size limit only prevents users from sending new mail until additional space is cleaned up.
    Danita

  • EPICS access issues via DSC Module - Array Size Limitations

    It seems that LabVIEW/DSC is not paying attention to the EPICS_CA_MAX_ARRAY_BYTES environment variable.  I have set it to a large value which works for caget and camonitor, but LV/DSC only allows me to read 4099 four byte floats in a single array.  If I ask for 4100 or more I get "Unknown Bad Quality" in the System Manager status.
    Is there some way I can get larger array bounds?  I have some quite large arrays (20,000+ floats) that I must read and process.

    Hello,
    Currently there is no workaround for this situation, but I can assure you that R&D is looking into it because there was CAR created for it by one of the R&D engineers. So I can assure you that it is being looked into.
    National Instruments
    RIO Embedded Hardware PSE
    CompactRIO Developers Guide

  • GC.log File Size Limitation and Rotation

    Hi,
    Is it possible to limit the size of a GC.log file and to rotate it without causing havoc?
    Best regards,
    Katzensee

    James Roller1 wrote:
    You say, if you use 1024 x 768 images... that's just it: I'm just using the images without resizing. Some may be 4MB files when dragged in; others may be 300kb. I am not doing anything to them. Should I?  Does that help? Or does IBA "handle all that" when I drag and drop?
    It's not well documented. I get the impression that, if you drop an image that has resolution higher than 2048 x 1536, it is down-sampled to that size. You should at least crop the image outside IBA before dropping it in. If you want to save space, down-sample the image first to 1024 x 768 (after cropping).
    James Roller1 wrote:
    Also for the video: we're using iMovie and we just "Share" to iTunes and choose Large. Should I be choosing a lower setting or, again, does IBA not care and will take the Large and resize it?
    Instead of sharing to iTunes, export the movie to iPad format (File -> Export) and select 640 x 480. That'll give you reasonable quality at around 12 MB per minute.
    James Roller1 wrote:
    I have to say I chuckled at the wisdom and simplicity of the 2 Volume suggestion. I'd like to keep it as one, and I'd like to sell it on the store, but your idea is ingenious in its simplicity: just make 2!
    Thanks! Two's better'n one
    Michi.

  • Array Size Bug PDA?

    I think I may have stumbled across (another) PDA bug.
    On my desktop the array size corectly returns the number of elements. On my PDA it returns zero - has naybody else witnessed this? My array is a 1D string with 8 elements.
    Thanks in advance.

    I have got to the bottom of this one! It appears that there is an inconsistency in the operation of the Spreadsheet String To Array function.
    For some reason I had made the array type terminal a plain string ( not an n-dimension array). On a PC this seems to work OK, when it is compiled for PDA it does not return an array.
    By changing the string to a 2D string array it works on both the PC & PDA.
    Does not work on PDA:
    Works on PDA
    ssk
    Message Edited by ssk on 03-05-2009 07:53 AM
    Attachments:
    Works.JPG ‏16 KB
    Does_Not_Work.JPG ‏15 KB

  • HTML Editor size limitation?

    Hi,<br>
    I'm using Apex 3.1 on Oracle 10g...<br>
    Is there a size limitation on the number of characters in the HTML Editor? <br>
    We have a Report/Form combo for a column. clicking on the "Edit" Link from the Report takes you to the Form where you can edit it. <br>
    The corresponding database column is of type LONG. I see the data in the report, but clicking on Edit gives me an error - <br>
    ORA-20001: Error fetching column value: ORA-06502: PL/SQL: numeric or value error: character string buffer too small <br>
    I've tried switching from CLOB to LONG for the column's datatype, but both fail to open the Form. <br>
    If I change the Item type to "Text Area", the LONG column works, but I do need it as an HTML Editor... <br>
    Thanks for any help!

    Scott (sspafado),
    Before you ask, my name is Viji
    :)

  • Oracle (Apache) HTTP Server - Default Header Size limitation

    Does the Oracle HTTP Server have a http header size limitation? If so, what is the maximum size allowed for http headers? Can it be changed, and how?
    Which version of Apache shipped with AS 9.0.4? I've been trying to find the answer to my questions in the Apache 1.3 documentation, but I'm not having any luck.
    Let me explain my problem. We're using Vintela's Single Sign-On library for authentication in our java applications. Recently we ran into a problem where a user was not being granted access to the application. Much debugging occured, eventually we had to open a trouble ticket with Vintela. They suggested it might be a http header size limitation and to check the configuration for the web cache and apache. We easily found the configuration options for the web cache, but are still looking for apache.
    We've by-passed the web cache and accessed the HTTP server directly and we are still experiancing the same problem.
    To keep this message short and concise, I've omitted most of our troubleshooting, we're pretty sure the problem is related to a HTTP header size limitation.

    One trick I saw the Oracle guys do is telnet to the httpd port and manually type in an HTTP request.
    Perhaps ask the Vintela people for a test string that you can paste in a telnet window to test if the server handles it correctly.
    Also, you can put Apache in a sort of debug mode as well using the AS Console. I can't remember, but I think this may show the entire http request including the headers.

  • Change array size and delete duplicate row

    Hello,
    I am running a program that does a for loop 3 times.  In each loop, data is collected for x and y points (10 in the example provided).  How can I output the array with the 30 points in a simple 2D array (size 2x30)?
    Also, from the array, how do I delete duplicate rows that contain the same x-value? 
    Thanks,
    hiNi.
    Solved!
    Go to Solution.
    Attachments:
    array size.vi ‏10 KB

    Hello,
    THANK YOU for solving my first issue!  Attached is the data set that contains the duplicate entries for the x-axis values.  The y-axis value may be different, but not the x.  I would like to delete the 2nd row that contains the same x-value.
    Here is simple example of my input and desired output:
    IN: 
    28.512000 -115.310532
    28.812000 -86.937660
    29.112000 -116.877052
    29.412000 -87.223625
    29.412000 -111.349045
    30.012000 -86.510780
    30.312000 -112.048187
    OUT:
    28.512000 -115.310532
    28.812000 -86.937660
    29.112000 -116.877052
    29.412000 -87.223625
    30.012000 -86.510780
    30.312000 -112.048187
    I am thinking of comparig the last x-axis value to the current one, and if it is the same, then not parse that row.  The attached VI is as far as I got.
    Any help will be greatly appreciated.
    Thanks,
    hiNi
    Attachments:
    delete duplicate row.vi ‏8 KB
    data.txt ‏1 KB

  • Size Limits to xmlgen.insertXML and Steves XMLLoader

    Hi,
    What are the size limits are for xmlgen.insertXML? Can you only insert a varchar sized document? i.e. not a clob directly? - It always seems to fail past a certain document size (about 1600 records of a table with one number column)
    I've also tried using XMLLoader from Steves book - but that failed on large tables too. (12,000 records and 20 columns).
    Any help much appreciated!
    Simon.
    null

    Hi,
    As everyone knows ;-) there is no memory probems in java, but...
    I was able load clobs and long varchar2 columns into the database, but it used to hung from time to time - I'm using the XSQL commandLine and servlet on both unix and NT.
    I've found out that some of the problems are related to the jdk, the hunging problem on mass load was solved when I moved to jdk 1.2.2... and no version earlier, also I think I've installed a solaris patch relating to java, but I don't realy remember.
    By the way, If you are using clobs try using thin driver instead of oci8 driver it solve some problems for me.
    It it always worse checking your rollback segments and so.
    Hadar

  • Multi Numeric Limit Test define Array Size of Limits from LabView

    Hello,
    i have a new problem. I want to change the array size of numericarray and Step.Result.Measurement from LabView.
    I want to do the same thing i can do when i use Edit Limits and press the button Add or Remove.
    When i only give them a define array from Labview it doesn't work.
    Thank you
    best reguards

    Dear Tumbler
    I think the following thread should answer your question:
    http://forums.ni.com/ni/board/message?board.id=330​&message.id=2826&requireLogin=False.
    Best regards
    Philipp Roessler

  • I recently downloaded a 10.6.8 update and a game program has now been reduced in size.  It's specifications are set for full screen, but it is about 1/4 of the size.  Any ideas how to fix?

    I recently downloaded a 10.6.8 update and a game program has now been reduced in size.  It's specifications are set for full screen, but it is about 1/4 of the size.  Any ideas how to fix?

    Hi, I have got the same problem but all is 1/4 the size have you fixed it?

  • I bought Photoshop CS6 Extended(education edition) for Mac. Now ik want to install it on my (new) macbook. I have the product code and serial number. I receive an error code: may be a false copy. How can I install this program?

    In 2012, I bought Photoshop CS6 Extended (education edition) for Mac. Now I want to install it on my new Macbook. I have the correct serial number and product code, but I receive an error message: the software may be a false copy etc....What to do?

    Here is a copy of the error
    This means : Installation failed.
    Verification of the Adobe Software failed
    The product you want to install is no valid Adobe product and seems to be falsified.
    HUgo
    Op 29-aug.-2014, om 23:42 heeft Jeff A Wright <[email protected]> het volgende geschreven:
    I bought Photoshop CS6 Extended(education edition) for Mac. Now ik want to install it on my (new) macbook. I have the product code and serial number. I receive an error code: may be a false copy. How can I install this program?
    created by Jeff A Wright in Downloading, Installing, Setting Up - View the full discussion
    Hugo please turn off your e-mail signature.
    If your serial number is listed as being valid at http://www.adobe.com/ then I would recommend obtaining a fresh copy of the installation files.  You can find details on how to locate your serial number at Find your serial number quickly - http://helpx.adobe.com/x-productkb/global/find-serial-number.html.
    To download a fresh copy of the installation files please see Download CS6 products.
    Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at https://forums.adobe.com/message/6685617#6685617
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.
    Start a new discussion in Downloading, Installing, Setting Up by email or at Adobe Community
    For more information about maintaining your forum email notifications please go to http://forums.adobe.com/thread/416458?tstart=0.

  • Nio ByteBuffer and memory-mapped file size limitation

    I have a question/issue regarding ByteBuffer and memory-mapped file size limitations. I recently started using NIO FileChannels and ByteBuffers to store and process buffers of binary data. Until now, the maximum individual ByteBuffer/memory-mapped file size I have needed to process was around 80MB.
    However, I need to now begin processing larger buffers of binary data from a new source. Initial testing with buffer sizes above 100MB result in IOExceptions (java.lang.OutOfMemoryError: Map failed).
    I am using 32bit Windows XP; 2GB of memory (typically 1.3 to 1.5GB free); Java version 1.6.0_03; with -Xmx set to 1280m. Decreasing the Java heap max size down 768m does result in the ability to memory map larger buffers to files, but never bigger than roughly 500MB. However, the application that uses this code contains other components that require the -xMx option to be set to 1280.
    The following simple code segment executed by itself will produce the IOException for me when executed using -Xmx1280m. If I use -Xmx768m, I can increase the buffer size up to around 300MB, but never to a size that I would think I could map.
    try
    String mapFile = "C:/temp/" + UUID.randomUUID().toString() + ".tmp";
    FileChannel rwChan = new RandomAccessFile( mapFile, "rw").getChannel();
    ByteBuffer byteBuffer = rwChan.map( FileChannel.MapMode.READ_WRITE,
    0, 100000000 );
    rwChan.close();
    catch( Exception e )
    e.printStackTrace();
    I am hoping that someone can shed some light on the factors that affect the amount of data that may be memory mapped to/in a file at one time. I have investigated this for some time now and based on my understanding of how memory mapped files are supposed to work, I would think that I could map ByteBuffers to files larger than 500MB. I believe that address space plays a role, but I admittedly am no OS address space expert.
    Thanks in advance for any input.
    Regards- KJ

    See the workaround in http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038

  • My CS2 has worked fine for serval years.  All of a suddend my user name, Alan Ford, and sereal number have vanished from my program.  How do I correct this?

    My CS2 has worked fine for serval years. All of a suddend my user name, Alan Ford, and sereal number have vanished from my program. How do I correct this?

    Hello,
    Why do you need to use the regasm utility from the post-build action?
    There is a difference between signing the assembly with a strong name and digital signature. The
    How to: Sign an Assembly with a Strong Name article in MSDN explains how to sign an assembly with a strong name (.snk). See
    How to digitally sign a strong named assembly for adding a digital signature.
    You may also find the
    What's the Difference, Part Five: certificate signing vs strong naming article helpful.

  • Two instances of quirky behavior: array size of empty array and tab control freeze

    LV 7.1
    Array size of empty array. Adding empty arrays to an array of higher dimension produces a fantom array of non-zero size.
    Tab control freeze. An event structure with checked 'lock front panel until the event case completes' option permanently freezes the front panel in certain special circumstances.
    Zador

    tst wrote:
     Tell me if you still think this makes sense.
    Whew, let me look at this tonight after I activate the 4D module in my brain.
    (I think it makes sense, though ) Generally I look at the product of the dimensions array to determine if an array is empty. You can initialize a multidimensional array with some dimensions at zero and it is reproduced nicely in the array size output. Also the "Empty array" tool correctly identifies it as empty, even if some dimensions are nonzero.
    Message Edited by altenbach on 03-15-200612:42 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    init.png ‏3 KB

Maybe you are looking for

  • Using External Monitor as display, and laptop for keyboard and track pad

    Hi, I have an aluminum Powerboook that is about two years old and I have it attached to a Samsung Syncmaster 203B external monitor. I have been looking for a set of instructions so that I can set the displays (I have been scouting around the preferen

  • Security - Preventing "Save As" Functionality

    I am a user of Version 8 and have established password security in its strictest form.  My problem is that the Save As functionality is not prevented.  So you can perform a Save As file operation.  The new file does retain all the password security t

  • How to get SQL script for generating table, constraint, indexes?

    I'd like to get from somewhere Oracle tool for generating simple SQL script for generating table, indexes, constraint (like Toad) and it has to be Oracle tool but not Designer. Can someone give me some edvice? Thanks! m.

  • Ability to create a poster of 650cm

    Hi guys, first of all... I apologize for my english; than... I have to create a poster of 650x90cm but it seems Adobe Illustrator allows a maximun dimension of 577.95cm. Do you know a way to work around that problem? Or, alternatively, another progra

  • How to check users inputs

    hi,there i am developing an on-line shop,at the end of checking out process , the users are supposed to submit a details form which including first name ,surname, address,email, phone no, card number etc. Can any one tell me how to check those detail