primes

C++ Vector Elements Count

六眼飞鱼酱① 提交于 2019-12-02 11:53:51
In C++, using the vector header, how do I find the number of elements? #include <iostream> #include <cmath> #include <fstream> #include <cstdlib> #include <vector> using namespace std; int primer(int max); int main() { system("pause"); return 0; primer(1000); } int primer(int max){ vector<int> a; a[1]=2; for (int i=2;i<=max;i++){ bool prime=true; for (int ii=1;ii<=#a;ii++) { if i/a[ii]==math.floor(i/a[ii]) { prime=false; } } if prime==true { a[#a+1]=i; } } for (i=1;i<=#a;i++) { cout << a[i]); } } } I originally wrote the code for lua, and this is my attempt to translate it to C++. I would

In haskell how would you go about generating a list of all prime numbers upto a number say x?

江枫思渺然 提交于 2019-12-02 09:43:20
so the function would be something like primesearch::Int -> [Int] . For example, primesearch 4 = [2,3,5,7] . Would you need to use the sieve function somehow? or is there another way of doing it? To generate the first k prime numbers, or the prime numbers <= n , I recommend a sieve. Which kind of sieve depends on how many primes you want. For small numbers of primes, a monolithic Eratosthenes bit sieve is simple and fast. But if you want large numbers of primes, a monolithic sieve would need too much memory, so a segmented sieve is then the better option. For very small numbers of primes (the

Datetime does not show up

て烟熏妆下的殇ゞ 提交于 2019-12-02 09:32:40
I am trying to get timestamp to show- I have tried the onCreate query in different ways and also tried to to have addTime as a value in addPrime . Nothing seems to work. My intention is for the app to show previous primes and the time that they were found. The intention for the app is for the user to be able to close/kill the app and resume counting from last found prime number when restarting the app, if you have any hints how also this would be possible I would be grateful. This is the PrimeDBManager class public class PrimeDBManager extends SQLiteOpenHelper { private static final int

printing out prime numbers from array

可紊 提交于 2019-12-02 08:06:26
I'd like to print out all prime numbers from an array with method. I can do it with one int but don't know how to return certain numbers from array. Thanks for help! public static boolean isPrime(int [] tab) { boolean prime = true; for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) if (tab[i] % i == 0) { prime = false; break; } for(int i=0; i<tab.length; i++) if (( tab[i]%2 !=0 && prime && tab[i] > 2) || tab[i] == 2) { return true; } else { return false; } //return prime; } thanks both of you. Seems like its solved: public static void isPrime(int[] tab) { for (int i = 0; i < tab.length; i++) { if

Find the highest prime number in a given range

末鹿安然 提交于 2019-12-02 08:04:12
I need to find the highest prime number in a given range. Here is my code which works for 0-100 but if I give 0-125 it is showing prime number as 125. <?php $flag=0; $b=125; for($i=$b;$i>=0;$i--) { if($i%2!=0) { for($b=3;$b<10;$b++) { if($flag==0) { echo('<br>'); if($i%$b!=0) { echo('highest prime number is'.$i); $flag=1; break; } elseif ($i%$b==0) { break; } } } } } ?> In the above code I have taken the range from 0-125 Yup the problem is algorithmic... 1) You'll need to check up till sqrt($b) i.e. 11 in this case 2) The $flag logic is kinda messed up, no use changing the flag then breaking

How to print prime numbers up to the user's entered integer?

ぃ、小莉子 提交于 2019-12-02 08:04:07
Good afternoon everyone, I'm currently trying to create a program that does the following: Develop a code that will print all prime numbers up to the user's entered number. An example of the output: Enter an integer (2 or above): 19 The prime numbers up to you integer are: 2 3 5 7 11 13 17 19 Unfortunately, there is also a list of "requirements" that need to be met as well: If the user enters a number below 2, your program should print a message that the number is not valid, and then stop. A number is a prime number if it is not divisible by any number except 1 and itself. For this program, in

Circular prime numbers

我的梦境 提交于 2019-12-02 07:40:44
问题 I am trying to convert the following function which test the number if it's prime to another one that test if the integer is a circular prime. eg. 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. So i need to rotate the digits of the integer and test if the number is prime or not. any ideas? note: I am new to Haskell Programming isPrime :: Integer -> Bool isPrime 1 = False isPrime 2 = True isPrime n | (length [x | x <- [2 .. n-1], n `mod` x == 0]) > 0 = False |

How come in this code to find prime numbers, is_prime(9) returns True? [duplicate]

和自甴很熟 提交于 2019-12-02 07:17:16
This question already has an answer here: Program that checks if a number is prime number 5 answers def is_prime(x): if x < 2: return False else: for n in range(2, x): if x % n == 0: return False else: return True print is_prime(9) returns True instead of False . I don't quite understand. The range (2,9) includes this list: 2,3,4,5,6,7,8 and 9 % 3 == 0 , So how come I do not get False as the answer of that function? This is because you don't actually loop, as you return True during the first cycle (9 % 2 == 0 is False). Something like this should solve the problem: def is_prime(x): if x < 2:

Sieve of Eratosthenes without arrays?

泪湿孤枕 提交于 2019-12-02 07:08:49
I have to write a java code for the 'sieve of eratosthenes' algorithm to print out primes up to a given max value on the console but I'm not allowed to use arrays. Our professor told us it is possible to do only with the help of loops. So I thought a lot and googled a lot about this topic and couldn't find an answer. I dont think it's possible at all because you have store the information which digits are already crossed out somewhere. my code until now: public static void main(String[] args) { int n = 100; int mark = 2; System.out.print("Primes from 1 to "+n+": 2, "); for (int i = 2; i <= n;

How to fix - 41: non-static variable cannot be referenced from a static context -> What is the reason for this?

馋奶兔 提交于 2019-12-02 06:51:24
I'm trying to write this code to get the first initialCapacity prime numbers and then print them in sequence using java. It isn't working for two reasons, firstly I get the error 41: non-static variable listOfPrimeNumbers cannot be referenced from a static context when I try to run the program, but even when I change the variable to static and run the program, it will only print out "1". So it is only iterating the while loop in the constructor Primes once, and then stopping and I simply cannot find the problem there no matter how hard I look ! Would anyone be able to help me out please, even