int count = 0; for(int i = 1; i <a.length; i++) { if(A[i] % 2 = 1) count++; } return count;

Would this work?

Posted 07 August 2010 - 11:50 AM

I know that the rule for prime is that the # can't be divisible by anything other then itself and 1. i.e. 5 / 2 = 2, 5 / 3 = 1, so we know that 5 is prime because only 5 x 1 can get us 5 so in writing java code lets say we have an array which is size n with numbers inside. And we wanted to check if each of those numbers were prime, and count how many were. Lets assume the array is called A and has n # of elements inside of it

Would this work?

int count = 0; for(int i = 1; i <a.length; i++) { if(A[i] % 2 = 1) count++; } return count;

Would this work?

Posted 07 August 2010 - 12:12 PM

Your code is messed up. What's the name of your array? Is it "a" or "A"? Java is case-sensitive, so be careful with variables' names. And, your condition in if statement is wrong either. If you want to compare whether the value of a[i] % 2 is 1 or not, you should use == instead of =. = is only used for assigning values to variables. The algorithm you're using can only be used to check whether the value of the variable is an odd number or even number. For prime number checking algorithms, you might wanna try Sieve of Erasthosthenes or Sieve of Atkin.

Posted 07 August 2010 - 12:19 PM

I guess not. I think the definition of prime is clear to you. Prime is a number which is divisible only by 1 and itself. That means moduler division of given number and any other number less than that will give you a non-zero integer. Here it is Prime number.

5 is a prime number because :

5%2!=0;

5%3!=0;

5%4!=0;

So you have an array of numbers. Define a function say isPrime(), pass each of the number into that function. Write for loop which iterate from 2 to that number and check weather or not that number gives 0 on moduler division with number less than it. If the mod is 0 then break the loop and mark it as composite. Now the numbers in the array that are not composite is prime.

Although the time complexity is highest for this test. If you traverse upto sqrt(n) this will minimize you time requirement and sqrt(n) is sufficient limit for primality checking. Although there are many other algorithms.

Try it.

Thank you.

5 is a prime number because :

5%2!=0;

5%3!=0;

5%4!=0;

So you have an array of numbers. Define a function say isPrime(), pass each of the number into that function. Write for loop which iterate from 2 to that number and check weather or not that number gives 0 on moduler division with number less than it. If the mod is 0 then break the loop and mark it as composite. Now the numbers in the array that are not composite is prime.

Although the time complexity is highest for this test. If you traverse upto sqrt(n) this will minimize you time requirement and sqrt(n) is sufficient limit for primality checking. Although there are many other algorithms.

Try it.

Thank you.

Posted 07 August 2010 - 12:24 PM

I've seen this article but it doesn't show any where how to write java for it. I just need a quick solution I can study for a java final something simple thats all I'm looking for its sad he is having us memorize such a stupid procedure to begin with for an algorithms class

Posted 07 August 2010 - 12:25 PM

I think implementation of Sieve of Atkin is harder than Sieve of Erathosthenes. Normal implementation of Sieve of Atkin which is shown in wiki link gives slower generation of prime than that of Sieve of Eratosthenes. Also Sieve of Eratosthenes is good for number less than 10,000,000,000. But there is a chance of getting memory error if you try this huge range, without taking any remedial measure.

Posted 08 August 2010 - 05:35 AM

For checking a number for being prime, you know the concept but can't implement it properly. Dry-run the code in your mind once, and you'll get what I mean. See, you are dividing each element of the array by 2 and increasing the counter if it is. So, the counter actually counts the number of even numbers in the program.

Therefore, you should run another loop nested inside this loop. The outer loop (say, i loop) runs from 0 to a.length - 1 (because indexing starts from 0), while the inner loop (say, j loop) from 1 to a[i]. Hence, for every element, it will run from 1 to that element, and the counter will increase if the element is divisible by any number lying in between 1 and that number. Outer the inner loop (but still inside the outer loop) print a[i] if the counter is true.

I guess using A instead of a in the condition is just a typo, because remember Java is a case-sensitive language. And also remember if you use the = operator in the condition, it will almost always be true as it will set the value of that variable to something. So, use ==, which is for checking.

If you still have problems implementing the logic, I might help again.

Therefore, you should run another loop nested inside this loop. The outer loop (say, i loop) runs from 0 to a.length - 1 (because indexing starts from 0), while the inner loop (say, j loop) from 1 to a[i]. Hence, for every element, it will run from 1 to that element, and the counter will increase if the element is divisible by any number lying in between 1 and that number. Outer the inner loop (but still inside the outer loop) print a[i] if the counter is true.

I guess using A instead of a in the condition is just a typo, because remember Java is a case-sensitive language. And also remember if you use the = operator in the condition, it will almost always be true as it will set the value of that variable to something. So, use ==, which is for checking.

If you still have problems implementing the logic, I might help again.

This post has been edited by **adhish94**: 08 August 2010 - 05:43 AM

Posted 08 August 2010 - 06:36 AM

tscott, on 07 August 2010 - 10:50 AM, said:

int count = 0; for(int i = 1; i <a.length; i++) { if(A[i] % 2 = 1) count++; } return count;

Would this work?

First off

if(A[i] % 2 == 1)This modified line says: Is value of the array an odd number(1,3,5,7,9... 2k+1)? if yes: count++. So, it wont work. What you should do is:

for(int i = 0; i <a.length; i++)Every array starts with an element 0, not 1.

if(Prime(A[i])==true) count++;If A[i] is indeed a prime number, increment the count var. And the last piece of code, the Prime function:

boolean Prime(int number){ //Check if number is a prime number // return true/false; }

Posted 08 August 2010 - 07:41 AM

It's as simple as starting with the number in question then setting up a for loop to run from the number in question down to 1.

you have to check if the number in question is divisible by any number between itself and 1, otherwise, your data is not complete

you have to check if the number in question is divisible by any number between itself and 1, otherwise, your data is not complete

Posted 08 August 2010 - 07:17 PM

Tapas Bose, on 07 August 2010 - 01:25 PM, said:

I think implementation of Sieve of Atkin is harder than Sieve of Erathosthenes. Normal implementation of Sieve of Atkin which is shown in wiki link gives slower generation of prime than that of Sieve of Eratosthenes. Also Sieve of Eratosthenes is good for number less than 10,000,000,000. But there is a chance of getting memory error if you try this huge range, without taking any remedial measure.

Actually it is 2,xxx,xxx,xxx

Here is a solution to avoid that problem

http://www.dreaminco...snippet4665.htm

Page 1 of 1

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- Python
- PHP
- Mobile Development
- ASP.NET
- .NET Framework
- Ruby
- Game Development
- Assembly
- Databases
- ColdFusion
- VB6
- Other Languages
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- I need help with this Array Question
- Dijkstra's Algorithm
- Book Review: Murach's Beginning Java with NetBeans
- Graph Data Structure Tutorial
- Swing, Passive Model-View-Presenter in 5 minutes.
- Book Review: Murach's Java Servlets and JSP
- Phobos - A JavaFX Games Engine: Part 2 - JavaFX Scene API and the FSM
- Maven Tutorial 2 - Adding Dependencies
- Maven Tutorial 1 - Installation and Getting Started
- Phobos - A JavaFX Games Engine: Part 1 - Intro to Threading and DP
**221 More Java Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- ASP.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- ASP Snippets
- Linux, Unix, and Bash Snippets
- Other Languages Snippets
- Regex

Copyright 2001-2017 **MediaGroup1 LLC**, All Rights Reserved

A**MediaGroup1 LLC** Production - Version 6.0.2.1.36

Server: secure3

A

Server: secure3