How can i do that counting limits take too much time for big integers?










1















Im Vladimir Grygov and I have very serious problem.



In our work we now work on really hard algorithm, which using limits to cout the specific result.



Alghoritm is veary heavy and after two months of work we found really serious problem. Our team of analytics told me to solve this problem.



For the first I tell you the problem, which must be solve by limits:
We have veary much datas in the database. Ec INT_MAX.
For each this data we must sort them by the alghoritm to two groups and one must have red color interpretation and second must be blue.



The algorithm counts with ID field, which is some AUTO_INCREMENT value. For this value we check, if this value is eequal to 1. If yeas, this is red color data. If it is zero, this is blue data. If it is more. Then one, you must substract number 2 and check again.



We choose after big brainstorming method by for loop, but this was really slow for bigger number. So we wanted to remove cycle, and my colegue told me use recursion.



I did so. But... after implementation I had got unknown error for big integers and for example long long int and after him was wrote that: "Stack Overflow Exception"



From this I decided to write here, because IDE told me name of this page, so I think that here may be Answer.



Thank You so much. All of you.










share|improve this question






















  • It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

    – Christopher
    Nov 15 '18 at 0:01












  • Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

    – Vladimir Grygov
    Nov 16 '18 at 16:46
















1















Im Vladimir Grygov and I have very serious problem.



In our work we now work on really hard algorithm, which using limits to cout the specific result.



Alghoritm is veary heavy and after two months of work we found really serious problem. Our team of analytics told me to solve this problem.



For the first I tell you the problem, which must be solve by limits:
We have veary much datas in the database. Ec INT_MAX.
For each this data we must sort them by the alghoritm to two groups and one must have red color interpretation and second must be blue.



The algorithm counts with ID field, which is some AUTO_INCREMENT value. For this value we check, if this value is eequal to 1. If yeas, this is red color data. If it is zero, this is blue data. If it is more. Then one, you must substract number 2 and check again.



We choose after big brainstorming method by for loop, but this was really slow for bigger number. So we wanted to remove cycle, and my colegue told me use recursion.



I did so. But... after implementation I had got unknown error for big integers and for example long long int and after him was wrote that: "Stack Overflow Exception"



From this I decided to write here, because IDE told me name of this page, so I think that here may be Answer.



Thank You so much. All of you.










share|improve this question






















  • It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

    – Christopher
    Nov 15 '18 at 0:01












  • Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

    – Vladimir Grygov
    Nov 16 '18 at 16:46














1












1








1


1






Im Vladimir Grygov and I have very serious problem.



In our work we now work on really hard algorithm, which using limits to cout the specific result.



Alghoritm is veary heavy and after two months of work we found really serious problem. Our team of analytics told me to solve this problem.



For the first I tell you the problem, which must be solve by limits:
We have veary much datas in the database. Ec INT_MAX.
For each this data we must sort them by the alghoritm to two groups and one must have red color interpretation and second must be blue.



The algorithm counts with ID field, which is some AUTO_INCREMENT value. For this value we check, if this value is eequal to 1. If yeas, this is red color data. If it is zero, this is blue data. If it is more. Then one, you must substract number 2 and check again.



We choose after big brainstorming method by for loop, but this was really slow for bigger number. So we wanted to remove cycle, and my colegue told me use recursion.



I did so. But... after implementation I had got unknown error for big integers and for example long long int and after him was wrote that: "Stack Overflow Exception"



From this I decided to write here, because IDE told me name of this page, so I think that here may be Answer.



Thank You so much. All of you.










share|improve this question














Im Vladimir Grygov and I have very serious problem.



In our work we now work on really hard algorithm, which using limits to cout the specific result.



Alghoritm is veary heavy and after two months of work we found really serious problem. Our team of analytics told me to solve this problem.



For the first I tell you the problem, which must be solve by limits:
We have veary much datas in the database. Ec INT_MAX.
For each this data we must sort them by the alghoritm to two groups and one must have red color interpretation and second must be blue.



The algorithm counts with ID field, which is some AUTO_INCREMENT value. For this value we check, if this value is eequal to 1. If yeas, this is red color data. If it is zero, this is blue data. If it is more. Then one, you must substract number 2 and check again.



We choose after big brainstorming method by for loop, but this was really slow for bigger number. So we wanted to remove cycle, and my colegue told me use recursion.



I did so. But... after implementation I had got unknown error for big integers and for example long long int and after him was wrote that: "Stack Overflow Exception"



From this I decided to write here, because IDE told me name of this page, so I think that here may be Answer.



Thank You so much. All of you.







time stack limit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 21:12









Vladimir GrygovVladimir Grygov

82




82












  • It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

    – Christopher
    Nov 15 '18 at 0:01












  • Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

    – Vladimir Grygov
    Nov 16 '18 at 16:46


















  • It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

    – Christopher
    Nov 15 '18 at 0:01












  • Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

    – Vladimir Grygov
    Nov 16 '18 at 16:46

















It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

– Christopher
Nov 15 '18 at 0:01






It is really hard to read what you are doing. But as you mentiond "recursion" and "StackOverflowException" it is highly likely that you are running into a infinite recursion. The endpoint is the most important part of recursion. There are also problems that can not be solved by recursion or loops - they must be solved the other way by nature. It would really help if you posted any form of code.

– Christopher
Nov 15 '18 at 0:01














Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

– Vladimir Grygov
Nov 16 '18 at 16:46






Code, which creatinf the exception is: public bool isRed(long long val) if (val==1) return true; else if (val==0) return false; else return isRed(val - 2);

– Vladimir Grygov
Nov 16 '18 at 16:46













1 Answer
1






active

oldest

votes


















0














After your comment I think I can solve it:



public bool isRed(long long val) 
if (val==1)
return true;
else if (val==0)
return false;
else return isRed(val - 2);



Any halfway decent value for val will easily break this. There is just no way this could have worked with recursion. No CPU will support a stacktrace close to half long.MaxInt!



However there are some general issues with your code:



  1. Right now this is the most needlesly complex "is the number even" check ever. Most people use Modulo to figure that out. if(val%2 == 0) return false; else return true;

  2. the type long long seems off. Did you repeat the type? Did you mean to use BigInteger?


  3. If the value you substract by is not static and it is not solveable via modulo, then there is no reason not to use a loop here.



    public bool isRed (long long val)
    for(;val >= 0; val = val -2)
    if(value == 0)
    return false;

    return true;







share|improve this answer























  • Yea!!! It works!!! I love YOU!!!!

    – Vladimir Grygov
    Nov 16 '18 at 17:39











  • Just one more thing. Why don't you used in point 1 this: return val & 1; ???

    – Vladimir Grygov
    Nov 16 '18 at 17:40











  • @That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

    – Christopher
    Nov 17 '18 at 17:11










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308784%2fhow-can-i-do-that-counting-limits-take-too-much-time-for-big-integers%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









0














After your comment I think I can solve it:



public bool isRed(long long val) 
if (val==1)
return true;
else if (val==0)
return false;
else return isRed(val - 2);



Any halfway decent value for val will easily break this. There is just no way this could have worked with recursion. No CPU will support a stacktrace close to half long.MaxInt!



However there are some general issues with your code:



  1. Right now this is the most needlesly complex "is the number even" check ever. Most people use Modulo to figure that out. if(val%2 == 0) return false; else return true;

  2. the type long long seems off. Did you repeat the type? Did you mean to use BigInteger?


  3. If the value you substract by is not static and it is not solveable via modulo, then there is no reason not to use a loop here.



    public bool isRed (long long val)
    for(;val >= 0; val = val -2)
    if(value == 0)
    return false;

    return true;







share|improve this answer























  • Yea!!! It works!!! I love YOU!!!!

    – Vladimir Grygov
    Nov 16 '18 at 17:39











  • Just one more thing. Why don't you used in point 1 this: return val & 1; ???

    – Vladimir Grygov
    Nov 16 '18 at 17:40











  • @That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

    – Christopher
    Nov 17 '18 at 17:11















0














After your comment I think I can solve it:



public bool isRed(long long val) 
if (val==1)
return true;
else if (val==0)
return false;
else return isRed(val - 2);



Any halfway decent value for val will easily break this. There is just no way this could have worked with recursion. No CPU will support a stacktrace close to half long.MaxInt!



However there are some general issues with your code:



  1. Right now this is the most needlesly complex "is the number even" check ever. Most people use Modulo to figure that out. if(val%2 == 0) return false; else return true;

  2. the type long long seems off. Did you repeat the type? Did you mean to use BigInteger?


  3. If the value you substract by is not static and it is not solveable via modulo, then there is no reason not to use a loop here.



    public bool isRed (long long val)
    for(;val >= 0; val = val -2)
    if(value == 0)
    return false;

    return true;







share|improve this answer























  • Yea!!! It works!!! I love YOU!!!!

    – Vladimir Grygov
    Nov 16 '18 at 17:39











  • Just one more thing. Why don't you used in point 1 this: return val & 1; ???

    – Vladimir Grygov
    Nov 16 '18 at 17:40











  • @That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

    – Christopher
    Nov 17 '18 at 17:11













0












0








0







After your comment I think I can solve it:



public bool isRed(long long val) 
if (val==1)
return true;
else if (val==0)
return false;
else return isRed(val - 2);



Any halfway decent value for val will easily break this. There is just no way this could have worked with recursion. No CPU will support a stacktrace close to half long.MaxInt!



However there are some general issues with your code:



  1. Right now this is the most needlesly complex "is the number even" check ever. Most people use Modulo to figure that out. if(val%2 == 0) return false; else return true;

  2. the type long long seems off. Did you repeat the type? Did you mean to use BigInteger?


  3. If the value you substract by is not static and it is not solveable via modulo, then there is no reason not to use a loop here.



    public bool isRed (long long val)
    for(;val >= 0; val = val -2)
    if(value == 0)
    return false;

    return true;







share|improve this answer













After your comment I think I can solve it:



public bool isRed(long long val) 
if (val==1)
return true;
else if (val==0)
return false;
else return isRed(val - 2);



Any halfway decent value for val will easily break this. There is just no way this could have worked with recursion. No CPU will support a stacktrace close to half long.MaxInt!



However there are some general issues with your code:



  1. Right now this is the most needlesly complex "is the number even" check ever. Most people use Modulo to figure that out. if(val%2 == 0) return false; else return true;

  2. the type long long seems off. Did you repeat the type? Did you mean to use BigInteger?


  3. If the value you substract by is not static and it is not solveable via modulo, then there is no reason not to use a loop here.



    public bool isRed (long long val)
    for(;val >= 0; val = val -2)
    if(value == 0)
    return false;

    return true;








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 17:31









ChristopherChristopher

3,0062823




3,0062823












  • Yea!!! It works!!! I love YOU!!!!

    – Vladimir Grygov
    Nov 16 '18 at 17:39











  • Just one more thing. Why don't you used in point 1 this: return val & 1; ???

    – Vladimir Grygov
    Nov 16 '18 at 17:40











  • @That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

    – Christopher
    Nov 17 '18 at 17:11

















  • Yea!!! It works!!! I love YOU!!!!

    – Vladimir Grygov
    Nov 16 '18 at 17:39











  • Just one more thing. Why don't you used in point 1 this: return val & 1; ???

    – Vladimir Grygov
    Nov 16 '18 at 17:40











  • @That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

    – Christopher
    Nov 17 '18 at 17:11
















Yea!!! It works!!! I love YOU!!!!

– Vladimir Grygov
Nov 16 '18 at 17:39





Yea!!! It works!!! I love YOU!!!!

– Vladimir Grygov
Nov 16 '18 at 17:39













Just one more thing. Why don't you used in point 1 this: return val & 1; ???

– Vladimir Grygov
Nov 16 '18 at 17:40





Just one more thing. Why don't you used in point 1 this: return val & 1; ???

– Vladimir Grygov
Nov 16 '18 at 17:40













@That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

– Christopher
Nov 17 '18 at 17:11





@That sounds like a micro optimsiation. Tenerall I do not do those. readability ranks higher then miniscule performance gain. And if you need anything close to realtime, GC memory management and JiT compilers are usually a dead end anyway. Also a good place to post the speed rant: ericlippert.com/2012/12/17/performance-rant

– Christopher
Nov 17 '18 at 17:11



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308784%2fhow-can-i-do-that-counting-limits-take-too-much-time-for-big-integers%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto