How to do this? Arrays and math

This is a discussion on How to do this? Arrays and math within the C++ Programming forums, part of the General Programming Boards category; This is probably something simple, but know hardly any C++. What I need to do is: I have an __int64 ...

  1. #1
    brian0918
    Guest

    How to do this? Arrays and math

    This is probably something simple, but know hardly any C++.

    What I need to do is:

    I have an __int64 N (a 64 bit integer) that is set equal to a number. I want to mod (%) N with each number in this array:

    unsigned const long primetable[] =
    {
    3, 5, 7, 11, 13, 17, 19, 23,
    29, 31, 37, 41, 43, 47, 53, 59, 61
    };

    it starts with the first one, then loops to the next... and stops modding before the CURRENT NUMBER in the array gets greater than:

    (long)sqrt(N)

    I also have to be able to test each mod to see if the result is zero. It needs to be some kind of for-loop.

    Oh, also-- The array I have above isn't the complete one. The actual one is extremely long and contains thousands of separate numbers.

    What's stopping me is a lack of knowledge of the syntax, or anything about arrays or array manipulation. Any help would be great! Thanks.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    #include <iostream>
    
    #define NUM_ELEMENTS(n) (sizeof n / sizeof n[0])
    
    unsigned const long primetable[] =
    {
    2, 3, 5, 7, 11, 13, 17, 19, 23,
    29, 31, 37, 41, 43, 47, 53, 59, 61
    };
    
    int main() {
    	int test_min = 5, test_max = 500;
    
    	for (int someTest = test_min; someTest < test_max; someTest++) {
    		bool is_prime=true;
    		// breaks if sqrt(someTest) > last entry in primetable
    		for (int i = 0; i < NUM_ELEMENTS(primetable); ++i) {
    			if (someTest % primetable[i] == 0) {
    				is_prime=false;
    				break;
    			}
    		}
    		
    		if (is_prime) {
    			std::cout << someTest << " is prime" << std::endl;
    		} else {
    			std::cout << someTest << " is not prime" << std::endl;
    		}
    	}
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi-dimensional arrays Professionally
    By MGrancey in forum C++ Programming
    Replies: 20
    Last Post: 12-21-2008, 09:39 AM
  2. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 05:09 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. making a math quiz program
    By mackieinva in forum C Programming
    Replies: 10
    Last Post: 09-17-2007, 04:38 PM
  5. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21