Console Application is not running and shows nothing
The problem is to solve this.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I wrote this code
#include <iostream>
#include <math.h>
using namespace std;
bool prime(long int a);
int main()
{
long int b = 600851475143/2;
long int k;
for(long int i = 1; i <= b ; i++)
{
if(b % i == 0 && prime(i) == true)
{
k = i;
}
}
cout << k << endl;
return 0;
}
bool prime(long int a)
{
bool p = true;
for(long int i = 2; i <= sqrt(a) && p == true ; i++)
if(a % i == 0) p = false;
return p;
}
and when I execute after a build, it opens a console , and shows nothing
c++ console
add a comment |
The problem is to solve this.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I wrote this code
#include <iostream>
#include <math.h>
using namespace std;
bool prime(long int a);
int main()
{
long int b = 600851475143/2;
long int k;
for(long int i = 1; i <= b ; i++)
{
if(b % i == 0 && prime(i) == true)
{
k = i;
}
}
cout << k << endl;
return 0;
}
bool prime(long int a)
{
bool p = true;
for(long int i = 2; i <= sqrt(a) && p == true ; i++)
if(a % i == 0) p = false;
return p;
}
and when I execute after a build, it opens a console , and shows nothing
c++ console
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
Regarding theprime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing thei = 2
case.
– user4581301
Nov 25 '18 at 21:45
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01
add a comment |
The problem is to solve this.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I wrote this code
#include <iostream>
#include <math.h>
using namespace std;
bool prime(long int a);
int main()
{
long int b = 600851475143/2;
long int k;
for(long int i = 1; i <= b ; i++)
{
if(b % i == 0 && prime(i) == true)
{
k = i;
}
}
cout << k << endl;
return 0;
}
bool prime(long int a)
{
bool p = true;
for(long int i = 2; i <= sqrt(a) && p == true ; i++)
if(a % i == 0) p = false;
return p;
}
and when I execute after a build, it opens a console , and shows nothing
c++ console
The problem is to solve this.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I wrote this code
#include <iostream>
#include <math.h>
using namespace std;
bool prime(long int a);
int main()
{
long int b = 600851475143/2;
long int k;
for(long int i = 1; i <= b ; i++)
{
if(b % i == 0 && prime(i) == true)
{
k = i;
}
}
cout << k << endl;
return 0;
}
bool prime(long int a)
{
bool p = true;
for(long int i = 2; i <= sqrt(a) && p == true ; i++)
if(a % i == 0) p = false;
return p;
}
and when I execute after a build, it opens a console , and shows nothing
c++ console
c++ console
asked Nov 25 '18 at 21:31
Mane HambardzumyanMane Hambardzumyan
269
269
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
Regarding theprime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing thei = 2
case.
– user4581301
Nov 25 '18 at 21:45
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01
add a comment |
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
Regarding theprime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing thei = 2
case.
– user4581301
Nov 25 '18 at 21:45
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
Regarding the
prime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing the i = 2
case.– user4581301
Nov 25 '18 at 21:45
Regarding the
prime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing the i = 2
case.– user4581301
Nov 25 '18 at 21:45
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01
add a comment |
2 Answers
2
active
oldest
votes
Add a cout statement inside the for loop in main. Your program is running, it's just taking a long time.
add a comment |
The code is fine. 600851475143/2 is just a large number, so you have to wait some minutes till the result will be printed.
Further you're testing kind of twice if it a prime which makes the complexity unnecessarily much higher.
Try this:
long int b = 600851475143/2;
long int k = b;
for(long int i = 2; i < b ; i++)
{
if(b % i == 0)
{
k = i;
break;
}
}
cout << k << endl;
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
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%2f53472211%2fconsole-application-is-not-running-and-shows-nothing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add a cout statement inside the for loop in main. Your program is running, it's just taking a long time.
add a comment |
Add a cout statement inside the for loop in main. Your program is running, it's just taking a long time.
add a comment |
Add a cout statement inside the for loop in main. Your program is running, it's just taking a long time.
Add a cout statement inside the for loop in main. Your program is running, it's just taking a long time.
answered Nov 25 '18 at 21:38
SavithruSavithru
602511
602511
add a comment |
add a comment |
The code is fine. 600851475143/2 is just a large number, so you have to wait some minutes till the result will be printed.
Further you're testing kind of twice if it a prime which makes the complexity unnecessarily much higher.
Try this:
long int b = 600851475143/2;
long int k = b;
for(long int i = 2; i < b ; i++)
{
if(b % i == 0)
{
k = i;
break;
}
}
cout << k << endl;
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
add a comment |
The code is fine. 600851475143/2 is just a large number, so you have to wait some minutes till the result will be printed.
Further you're testing kind of twice if it a prime which makes the complexity unnecessarily much higher.
Try this:
long int b = 600851475143/2;
long int k = b;
for(long int i = 2; i < b ; i++)
{
if(b % i == 0)
{
k = i;
break;
}
}
cout << k << endl;
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
add a comment |
The code is fine. 600851475143/2 is just a large number, so you have to wait some minutes till the result will be printed.
Further you're testing kind of twice if it a prime which makes the complexity unnecessarily much higher.
Try this:
long int b = 600851475143/2;
long int k = b;
for(long int i = 2; i < b ; i++)
{
if(b % i == 0)
{
k = i;
break;
}
}
cout << k << endl;
The code is fine. 600851475143/2 is just a large number, so you have to wait some minutes till the result will be printed.
Further you're testing kind of twice if it a prime which makes the complexity unnecessarily much higher.
Try this:
long int b = 600851475143/2;
long int k = b;
for(long int i = 2; i < b ; i++)
{
if(b % i == 0)
{
k = i;
break;
}
}
cout << k << endl;
edited Nov 25 '18 at 21:46
answered Nov 25 '18 at 21:41
goarangoaran
446
446
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
add a comment |
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
I think the problem is given for this. Maybe there is more optimal way to solve this problem ?
– Mane Hambardzumyan
Nov 25 '18 at 21:45
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
@ManeHambardzumyan Look into cryptography. Factoring huge numbers into primes is something they do every day and they have a lot of tricks to make it easier to compute.
– user4581301
Nov 25 '18 at 21:59
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%2f53472211%2fconsole-application-is-not-running-and-shows-nothing%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
Debuggers are a brilliant tool for solving problems like this. Pop a breakpoint at the start of the program, start up the program, and start start stepping until the program blocks or starts going around in circles.
– user4581301
Nov 25 '18 at 21:36
g++ spits out warning: overflow in implicit constant conversion [-Woverflow] long int b = 600851475143/2; You should resolve this. Come to think of it, 600851475143/2; is a darn big number. You might want to reconsider using it as a loop bound.
– user4581301
Nov 25 '18 at 21:42
Regarding the
prime
computation, this is a brutally slow way to do this. Consider the number of times you are recomputing thei = 2
case.– user4581301
Nov 25 '18 at 21:45
I didn't complete the previous thought. Use memoization to reduce the amount of redundant work you have to do. See if you can take advantage of a Prime Number Sieve.
– user4581301
Nov 25 '18 at 22:01