Giving right answer for N = 2000000 but not passing test cases
Find the sum of all prime numbers not greater than N. For example if user input 5 then prime numbers are 2,3,5 and their sum is 10. It is not passing 4 test cases in which two of them are exceeding the time limit. I have tried several test cases and my code is working fine on them. Here is my code.
public static long sieve_of_eratosthenes(long n)
{
if (n == 1)
{
// If the user input 1.
return (0);
}
else
{
long sum = 0;
bool array = new bool[n + 1];
for (long i = 2; i <= n; i++)
{
// Setting all values to true.
array[i] = true;
}
// Eliminating the composite numbers.
for (long j = 2; j < Math.Sqrt(n); j++)
{
if (array[j])
{
long multiple = 1;
for (long k = (j * j); k <= n; k = (j * j) + (j * (multiple++)))
{
array[k] = false;
}
}
}
//Now we have the prime numbers. We just have to add them.
for (int z = 2; z <= n; z++)
{
if (array[z])
{
sum = sum + z;
}
}
return (sum);
}
}
static void Main(string args)
{
int noofcases = int.Parse(Console.ReadLine());
for( int i = 0; i < noofcases; i ++)
{
long entry = long.Parse(Console.ReadLine());
Console.WriteLine(sieve_of_eratosthenes(entry));
}
}
c#
|
show 1 more comment
Find the sum of all prime numbers not greater than N. For example if user input 5 then prime numbers are 2,3,5 and their sum is 10. It is not passing 4 test cases in which two of them are exceeding the time limit. I have tried several test cases and my code is working fine on them. Here is my code.
public static long sieve_of_eratosthenes(long n)
{
if (n == 1)
{
// If the user input 1.
return (0);
}
else
{
long sum = 0;
bool array = new bool[n + 1];
for (long i = 2; i <= n; i++)
{
// Setting all values to true.
array[i] = true;
}
// Eliminating the composite numbers.
for (long j = 2; j < Math.Sqrt(n); j++)
{
if (array[j])
{
long multiple = 1;
for (long k = (j * j); k <= n; k = (j * j) + (j * (multiple++)))
{
array[k] = false;
}
}
}
//Now we have the prime numbers. We just have to add them.
for (int z = 2; z <= n; z++)
{
if (array[z])
{
sum = sum + z;
}
}
return (sum);
}
}
static void Main(string args)
{
int noofcases = int.Parse(Console.ReadLine());
for( int i = 0; i < noofcases; i ++)
{
long entry = long.Parse(Console.ReadLine());
Console.WriteLine(sieve_of_eratosthenes(entry));
}
}
c#
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03
|
show 1 more comment
Find the sum of all prime numbers not greater than N. For example if user input 5 then prime numbers are 2,3,5 and their sum is 10. It is not passing 4 test cases in which two of them are exceeding the time limit. I have tried several test cases and my code is working fine on them. Here is my code.
public static long sieve_of_eratosthenes(long n)
{
if (n == 1)
{
// If the user input 1.
return (0);
}
else
{
long sum = 0;
bool array = new bool[n + 1];
for (long i = 2; i <= n; i++)
{
// Setting all values to true.
array[i] = true;
}
// Eliminating the composite numbers.
for (long j = 2; j < Math.Sqrt(n); j++)
{
if (array[j])
{
long multiple = 1;
for (long k = (j * j); k <= n; k = (j * j) + (j * (multiple++)))
{
array[k] = false;
}
}
}
//Now we have the prime numbers. We just have to add them.
for (int z = 2; z <= n; z++)
{
if (array[z])
{
sum = sum + z;
}
}
return (sum);
}
}
static void Main(string args)
{
int noofcases = int.Parse(Console.ReadLine());
for( int i = 0; i < noofcases; i ++)
{
long entry = long.Parse(Console.ReadLine());
Console.WriteLine(sieve_of_eratosthenes(entry));
}
}
c#
Find the sum of all prime numbers not greater than N. For example if user input 5 then prime numbers are 2,3,5 and their sum is 10. It is not passing 4 test cases in which two of them are exceeding the time limit. I have tried several test cases and my code is working fine on them. Here is my code.
public static long sieve_of_eratosthenes(long n)
{
if (n == 1)
{
// If the user input 1.
return (0);
}
else
{
long sum = 0;
bool array = new bool[n + 1];
for (long i = 2; i <= n; i++)
{
// Setting all values to true.
array[i] = true;
}
// Eliminating the composite numbers.
for (long j = 2; j < Math.Sqrt(n); j++)
{
if (array[j])
{
long multiple = 1;
for (long k = (j * j); k <= n; k = (j * j) + (j * (multiple++)))
{
array[k] = false;
}
}
}
//Now we have the prime numbers. We just have to add them.
for (int z = 2; z <= n; z++)
{
if (array[z])
{
sum = sum + z;
}
}
return (sum);
}
}
static void Main(string args)
{
int noofcases = int.Parse(Console.ReadLine());
for( int i = 0; i < noofcases; i ++)
{
long entry = long.Parse(Console.ReadLine());
Console.WriteLine(sieve_of_eratosthenes(entry));
}
}
c#
c#
edited Jan 1 at 15:19
Sentry
3,69822234
3,69822234
asked Jan 1 at 15:10
Riven CallahanRiven Callahan
85
85
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03
|
show 1 more comment
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03
|
show 1 more comment
1 Answer
1
active
oldest
votes
check the below code. I wrote simple logic which you can improve
public static class Int32Extension
{
public static bool IsPrime(this int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
}
then
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!(++i).IsPrime())
continue;
sum += i;
}
Console.WriteLine(sum);
}
Without using Extension Method
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!IsPrime(++i))
continue;
sum += i;
}
Console.WriteLine(sum);
}
.Net Fiddle Link : https://dotnetfiddle.net/rEBY9r
Edit : The IsPrime test uses Primality Test With Pseudocode
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996552%2fgiving-right-answer-for-n-2000000-but-not-passing-test-cases%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
check the below code. I wrote simple logic which you can improve
public static class Int32Extension
{
public static bool IsPrime(this int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
}
then
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!(++i).IsPrime())
continue;
sum += i;
}
Console.WriteLine(sum);
}
Without using Extension Method
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!IsPrime(++i))
continue;
sum += i;
}
Console.WriteLine(sum);
}
.Net Fiddle Link : https://dotnetfiddle.net/rEBY9r
Edit : The IsPrime test uses Primality Test With Pseudocode
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
add a comment |
check the below code. I wrote simple logic which you can improve
public static class Int32Extension
{
public static bool IsPrime(this int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
}
then
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!(++i).IsPrime())
continue;
sum += i;
}
Console.WriteLine(sum);
}
Without using Extension Method
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!IsPrime(++i))
continue;
sum += i;
}
Console.WriteLine(sum);
}
.Net Fiddle Link : https://dotnetfiddle.net/rEBY9r
Edit : The IsPrime test uses Primality Test With Pseudocode
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
add a comment |
check the below code. I wrote simple logic which you can improve
public static class Int32Extension
{
public static bool IsPrime(this int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
}
then
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!(++i).IsPrime())
continue;
sum += i;
}
Console.WriteLine(sum);
}
Without using Extension Method
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!IsPrime(++i))
continue;
sum += i;
}
Console.WriteLine(sum);
}
.Net Fiddle Link : https://dotnetfiddle.net/rEBY9r
Edit : The IsPrime test uses Primality Test With Pseudocode
check the below code. I wrote simple logic which you can improve
public static class Int32Extension
{
public static bool IsPrime(this int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
}
then
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!(++i).IsPrime())
continue;
sum += i;
}
Console.WriteLine(sum);
}
Without using Extension Method
public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
if (number % i == 0)
return false;
return true;
}
static void Main(string args)
{
int input = 5;
int sum = 0;
for (int i = 0; i < input;)
{
if (!IsPrime(++i))
continue;
sum += i;
}
Console.WriteLine(sum);
}
.Net Fiddle Link : https://dotnetfiddle.net/rEBY9r
Edit : The IsPrime test uses Primality Test With Pseudocode
edited Jan 1 at 15:59
answered Jan 1 at 15:30


SimonareSimonare
15.1k11840
15.1k11840
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
add a comment |
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
Thanks for helping, it looks like good code, but I feel it's probably not what the Op needs. It uses advanced language features like extensions and it doesn't help him to learn the basics. See How do I ask and answer homework questions
– Compufreak
Jan 1 at 15:34
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
thanks for the answer. but can you tell me the logic of the boundary variable. i will be very thankful for yours.
– Riven Callahan
Jan 1 at 15:38
check the included reference
– Simonare
Jan 1 at 15:47
check the included reference
– Simonare
Jan 1 at 15:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996552%2fgiving-right-answer-for-n-2000000-but-not-passing-test-cases%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your logic might work, but it is far from efficient. The usage of arrays is unnecessary here, just do the check for a prime-number in a for-loop and calculate the sum while doing it instead of using such huge arrays to store your results inbetween. This clogs your memory and massively hurts your performance.
– Compufreak
Jan 1 at 15:22
i only find Sieve's method for finding the prime numbers that's why i use arrays. can you please elaborate the method you are talking about.
– Riven Callahan
Jan 1 at 15:39
I suspect there is a rounding issue with the SQRT method : j < Math.Sqrt(n) Try rounding up using Celing method.
– jdweng
Jan 1 at 15:41
It didn't make any difference in the outcome. @jdweng
– Riven Callahan
Jan 1 at 15:49
You can speed up algorithm by eliminating all even numbers except 2. : int z = 3; z <= n; z += 2 and long j = 3; j < Math.Sqrt(n); j += 2
– jdweng
Jan 1 at 16:03