I have to sum all prime numbers below 2,000,000 but my code is giving the wrong result(1,179,908,154 right is 142,913,828,922), since it's working perfectly with lower values I can't figure out what's wrong.

```
#include <iostream>
using namespace std;
int main(){
unsigned int j, i=2,ans=2, interval=2000000;
while(i<=interval){
i++;
j=2;
while(i!=j){
if(i % j != 0)
j++;
else{
i++; j=2;}
}
if (i>=interval)
break;
cout << i<< endl;
ans+=i;
}
cout << ans;
cin.get();
return 0;
}
```

You are declaring `ans`

as `unsigned int`

, on most machines today, that's 32-bit, which can represent numbers from `0`

to `4294967295`

, but the sum of all prime numbers under two million is definitely way over `4294967295`

, try use `unsigned long long`

instead.

By the way, the algorithm you used is very inefficient, you may consider The sieve of Eratosthenes:

Similar Questions

public int Partition1 {get;set;} public int Partition1 {get;set;} private void SetPartitions(List<int> primeNumbers) { this.Partition1 = // get the product of the prime numbers closest to 10000

I seem to be getting all the prime numbers, but some composite numbers are showing up as well. Why? p=[2,] while len(p)<35: next = p[-1]+1 for i in range(len(p)): if next%p[i]==0: next+=1 i=0 p.app

I've been working on this for two days, can't get it, I need some help. Goal - Among other things... SUM all project timesheet entries SUM all project invoices Group these by project (projectID) and

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don't think that's the way my book wants it to be written. It mentions

When i run following query it shows only 1 row. Based on created_date condition, where as if i remove created_date condition and just keep status_change_date condition it shows 3 rows. I want to

I am making an application that tells you if the number you enter is prime or not.The problem I have is that it only asks once and terminates the program. How could I do to keep asking numbers and the

I'm writing this Java program that finds all the prime numbers between a given range. Because I'm dealing with really big numbers my code seems to be not fast enough and gives me a time error. Here is

My code works for numbers up to 300 or 20. But it doesn't work for 2000000. I tried using long instead, but it still didn't work. #include <iostream> bool prime(int i) { bool result = true; int

I can't find anyway to get just the numbers below 40 to be added. When I code it this way, it adds up all the numbers in the array. public static int underForty(int[] nums2) { int sum = 0; for ( int e

I am trying to learn z3, and this is the first program I write. In this exercise, I am trying to determine if x is prime. If x is prime, return SAT, otherwise, return UNSAT alongside with two of its

I am just starting out, and I've written the following code and have been trying to debug it for a few hours. I am not getting something very basic here. I want the code to give me a list of prime num

I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime numbers to print out. I have

Hello could you take a quick glance at my code and point out the mistake(s). I'm trying to calculate sum of n numbers going like this: 1- 1/2 + 1/3 - 1/4 ... etc... With the following code, I get 1.00

What is wrong with this code? $data455 = mysql_query(SELECT ROUND(SUM(gainloss), 4) FROM stats WHERE MONTH(date) = MONTH(CURRENT_TIMESTAMP) AND winloss = 'Win'); $info455 = mysql_fetch_array($data45

I am writing a program that finds prime numbers up to a specified limit. I have tried: Sub Main() Console.WriteLine(Enter the maximum) Dim primes As List(Of Integer) = New List(Of Integer) Dim m As

I am trying to find the nth( n <= 2000000) square free semi prime. I have the following code to do so. int k = 0; for(int i = 0; i <= 1000; i++) { for(int j = i +1 ; j <= 2500; j++ ) { semip

Posted query retrieving all the relevant fields but the SUM(af_py_commission) function causing problem as in and it simply add an extra zero at the end and then on the right end. e.g. if the answer i

I wrote the following program to display all prime numbers up to 150. It is not executing at all. what is so wrong with it? # include <stdio.h> int main(void) { int p[150], i, j; for (i = 2; i &

I've written a simple program designed to compute and then sum prime numbers below a certain value (1000000 in this case). I tested it for low ranges and it seemed fine, so I'm fairly confident with t

Given a number N I need to find the count of the numbers that have atleast one prime digit (2,3,5 or 7) from 1 to N. Now N can be upto 10^18.What is the best approach to solve this problem. Example :

I am trying to create a procedure which builds a list of prime numbers from 2 to any number n. Of course, this needs to be done iteratively, but I do not exactly know what I'm doing wrong. (define (l

Given an integer M. return all prime numbers smaller than M. Give a algorithm as goo as you can. Need to consider time and space complexity. Anybody can drop a through? Appreciate!

I have passed below AmountDue in query but am getting wrong result. AmountDue data type is Float. AmountDue: 2412880.28 AmountDue: 561.06 My query: select CONVERT(varchar,(select convert(bigint,Amoun

hello every one I have written code char sentence[100]; scanf(%s ,sentence); char * ptrs = sentence ; printf(%d, strlen(ptrs)); suppose I enter john is a boy the strlen() function is giving me

What is the sum of all prime numbers between 1,000,000,000,000 and 1,000,000,100,000? this works out but is very slow.I need to optimize it.I am new to python. 3614000181007876 is the right answer A=

Currently I'm working on an iOS application, in which I need to convert a NSString to char *. For this I used the following code: NSString *str = @Hello ! How Are You ???; char *as = (char *)[[str d

I am trying to sum all the numbers up to a range, with all the numbers up to the same range. I am using python: limit = 10 sums = [] for x in range(1,limit+1): for y in range(1,limit+1): sums.append(x

I have a string which looks like this 12345 45678 25846 44447 Is there some way to make total sum of these numbers in php? I am trying to make total price from these numbers. Thank you.

I am trying to write a little script myself to compute all of the prime numbers up to n (a user submitted argument) and would appreciate a little bit of help. I want to use ArrayLists to write this fu

I am wondering why the code below, using the Mersenne Twister (in java), always gives the same 99 numbers no matter how many times I run it. In essence it isn't random? MersenneTwister mt = new Merse

for n in range(2,10): for x in range(2,n): if n%x==0: print(n, 'equals to', x, '*', n//x) break else: print(n, 'is a prime number') Below is the output I received. 3 is a prime number 4 equals to 2 *

This application will receive a number n. After receiving this number, the program has to show the nth prime in the list of primes. For example, if the user enters 3, the program is supposed to di

I am trying to solve the problem mentioned here: https://www.spoj.pl/problems/PRIME1/ I am also giving the description below. Peter wants to generate some prime numbers for his cryptosystem. Help him

I have used Sieve of Eratosthenes Algorithm to find the sum of prime numbers under certain limit and it has worked correctly until limit of 2 million, but when I tried 3 million the program has stoppe

I'm newly new to C programming , started about a week ago ! And i'm getting into loops i faced a problem writing a C program to show prime numbers less-than 100 . I'll share up the C code i've written

And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers fr

I have a program that generates prime numbers. This code works fine when i want the first 100 and 200 primes, but puts out a Floating point exception whenever I use a value above 300 for total. The pr

How to find increasing subsequence of numbers with maximum sum. I find O(N^2) but I want to know O(N log N). Thanks!

Build a program that will generate the sum and product of 20 input numbers using the whileâ€¦loop structure. Conditions: user need to input only 20 numbers and get the sum and product of all the number

I'm trying to send, the numbers of every cell, and calculate the sum of them in php. Then show the result in text input But it did not work.How Can I do it? This is my php code: <?php if(isset($_PO

Why List of Task resulting wrong result. Anyone can give direction? This my code so far. static string[] kk = new string[] { a, b, c, d, e, f }; static void Main(string[] args) { //Paralle

What the title says. Example excerpted from the haskellwiki prime numbers page: {-# OPTIONS_GHC -O2 -fno-cse #-} primesTME () = 2 : ([3,5..] `minus` join [[p*p,p*p+2*p..] | p <- primes']) where ..

The following code is giving false result all the time. I think i am comparing two numeric variables. is that not so? var request; var getfilesize; var maxdisplaysize=2097152; request = $.ajax({ type:

A prime heavy number is defined to be one that is the sum of more than one pair of prime numbers. Recall that a prime number is a number greater than 1 whose only divisors are 1 and itself. For exampl

This question already has an answer here: Why are these numbers not equal? 3 answers I am experiencing something weird with R recently. It can't get a sum write (even a simple one), I looked fo

I was working on a Project Euler challenge and solved it. Then a friend suggested that I only compare potential prime numbers to known primes. His solution is done in about half the time. I went back

I want to print all prime numbers that are 2-digits long. Here is my code: for(int input = 11; input <= 99; input += 2){ for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){ if(input%x != 0){ Sy

As we know DH algorithm based upon 2 large prime numbers, which are used to be called as prime and base I am writing app which implements DH key exchange algorithm. From security point of view should

In Javascript how would i find prime numbers between 0 - 100? i have thought about it, and i am not sure how to find them. i thought about doing x % x but i found the obvious problem with that. this i

I have written following assembly code for adding 10 numbers.I am able to compile it and execute it but I am getting wrong result. I just wanted to know how do I print the value of total on scree. sec