timeout for large input in bigintegerHow can I “delimit” an integer from a given string?Java 7: An array as inputPrinting an array with no elements?Java - Method executed prior to Default ConstructorKeep getting this error message for my RockPaperScissor Program. How do I fix this error?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?Annotation processing and processor registration in eclipse oxygen
Why did pressing the joystick button spit out keypresses?
How would a drone work in centrifugal force generated "gravity"?
Can Ogre clerics use Purify Food and Drink on humanoid characters?
Links to webpages in books
Should my manager be aware of private LinkedIn approaches I receive? How to politely have this happen?
Hand soldering SMD 1206 components
How dangerous are set-size assumptions?
How to split an equation in two lines?
Are all instances of trolls turning to stone ultimately references back to Tolkien?
Why do some professors with PhDs leave their professorships to teach high school?
If I wouldn't want to read the story, is writing it still a good idea?
Graphical representation of connection of people
What is the origin of Scooby-Doo's name?
C-152 carb heat on before landing in hot weather?
Inverse-quotes-quine
Set multicolumn to a exact width
Apply brace expansion in "reverse order"
Did Karl Marx ever use any example that involved cotton and dollars to illustrate the way capital and surplus value were generated?
Suggested order for Amazon Prime Doctor Who series
How was Hillel permitted to go to the skylight to hear the shiur
Are there any efficient algorithms to solve longest path problem in networks with cycles?
Should I prioritize my 401(k) over my student loans?
Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?
Why doesn't a marching band have strings?
timeout for large input in biginteger
How can I “delimit” an integer from a given string?Java 7: An array as inputPrinting an array with no elements?Java - Method executed prior to Default ConstructorKeep getting this error message for my RockPaperScissor Program. How do I fix this error?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?Annotation processing and processor registration in eclipse oxygen
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Consider a permutation of numbers 1 to n written on a paper. Let’s denote the product of its element as p and the sum of its elements as s. Given a positive integer n, your task is to determine whether p is divisible by s or not.
i tried by using bigInteger concept but out of 50 test case 30 is successfully passed but rest of them are showing timeout which may be because of recursion.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static BigInteger factorial(int n)
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int nn=n*(n+1)/2;
BigInteger sum=BigInteger.valueOf(nn);
BigInteger p=factorial(n);
if((p.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
else
System.out.println("NO");
for the sample test case is like
input is 3 and its output should be "YES".since (1+2+3) divides (1*2*3).
java biginteger
|
show 4 more comments
Consider a permutation of numbers 1 to n written on a paper. Let’s denote the product of its element as p and the sum of its elements as s. Given a positive integer n, your task is to determine whether p is divisible by s or not.
i tried by using bigInteger concept but out of 50 test case 30 is successfully passed but rest of them are showing timeout which may be because of recursion.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static BigInteger factorial(int n)
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int nn=n*(n+1)/2;
BigInteger sum=BigInteger.valueOf(nn);
BigInteger p=factorial(n);
if((p.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
else
System.out.println("NO");
for the sample test case is like
input is 3 and its output should be "YES".since (1+2+3) divides (1*2*3).
java biginteger
1
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
2
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
Also, you might get an integer overflow while calculatingnn
, better uselong
or also ´BigInteger. What is the upper-bound for
n`, anyway?
– tobias_k
Mar 25 at 10:12
1
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44
|
show 4 more comments
Consider a permutation of numbers 1 to n written on a paper. Let’s denote the product of its element as p and the sum of its elements as s. Given a positive integer n, your task is to determine whether p is divisible by s or not.
i tried by using bigInteger concept but out of 50 test case 30 is successfully passed but rest of them are showing timeout which may be because of recursion.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static BigInteger factorial(int n)
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int nn=n*(n+1)/2;
BigInteger sum=BigInteger.valueOf(nn);
BigInteger p=factorial(n);
if((p.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
else
System.out.println("NO");
for the sample test case is like
input is 3 and its output should be "YES".since (1+2+3) divides (1*2*3).
java biginteger
Consider a permutation of numbers 1 to n written on a paper. Let’s denote the product of its element as p and the sum of its elements as s. Given a positive integer n, your task is to determine whether p is divisible by s or not.
i tried by using bigInteger concept but out of 50 test case 30 is successfully passed but rest of them are showing timeout which may be because of recursion.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static BigInteger factorial(int n)
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int nn=n*(n+1)/2;
BigInteger sum=BigInteger.valueOf(nn);
BigInteger p=factorial(n);
if((p.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
else
System.out.println("NO");
for the sample test case is like
input is 3 and its output should be "YES".since (1+2+3) divides (1*2*3).
java biginteger
java biginteger
edited Mar 25 at 9:45
Paplusc
4841 gold badge6 silver badges15 bronze badges
4841 gold badge6 silver badges15 bronze badges
asked Mar 25 at 9:37
Satyanarayanareddy TadiSatyanarayanareddy Tadi
409 bronze badges
409 bronze badges
1
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
2
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
Also, you might get an integer overflow while calculatingnn
, better uselong
or also ´BigInteger. What is the upper-bound for
n`, anyway?
– tobias_k
Mar 25 at 10:12
1
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44
|
show 4 more comments
1
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
2
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
Also, you might get an integer overflow while calculatingnn
, better uselong
or also ´BigInteger. What is the upper-bound for
n`, anyway?
– tobias_k
Mar 25 at 10:12
1
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44
1
1
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
2
2
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
Also, you might get an integer overflow while calculating
nn
, better use long
or also ´BigInteger. What is the upper-bound for
n`, anyway?– tobias_k
Mar 25 at 10:12
Also, you might get an integer overflow while calculating
nn
, better use long
or also ´BigInteger. What is the upper-bound for
n`, anyway?– tobias_k
Mar 25 at 10:12
1
1
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44
|
show 4 more comments
3 Answers
3
active
oldest
votes
Try to remove the recursion and use the for loop to calculate the factorial.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static void factorial(long n, long nn)
BigInteger answer=new BigInteger("1");
BigInteger sum=BigInteger.valueOf(nn);
int foundMatch =0;
for(long i=n;i>0;i--)
answer=answer.multiply(new BigInteger(String.valueOf(i)));
if((answer.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
foundMatch = 1;
break;
if(foundMatch!=1)
System.out.println("NO");
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long nn=n*(n+1)/2;
factorial(n, nn);
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting along
toBigInteger
long after it has overflows (probably multiple times) does not really help. UseBigInteger
from the beginning.
– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
|
show 8 more comments
You can use the logic that if the intermediate product of the factorial is divisible by the sum, then the entire factorial is also divisible by sum.
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass
static boolean isFactorialDivisible(int n, BigInteger sum)
BigInteger p = BigInteger.ONE;
for (int i = n; i > 0; i--)
p = p.multiply(BigInteger.valueOf(n));
if (p.mod(sum).equals(BigInteger.ZERO))
return true;
BigInteger gcd = p.gcd(sum);
if (!gcd.equals(BigInteger.ONE))
p = p.divide(gcd);
sum = sum.divide(gcd);
return false;
public static void main(String args[]) throws Exception
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n * (n + 1) / 2;
BigInteger sum = BigInteger.valueOf(nn);
boolean p = isFactorialDivisible(n, sum);
if (p)
System.out.println("YES");
else
System.out.println("NO");
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
add a comment |
import java.util.*;
class TestClass
public static int prime(int n)
int count=0,flag=0;
if(n==1)
flag=1;
if(n==2)
flag=0;
else
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
flag=1;
break;
if(flag==1)
return 1;
return 0;
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
int flag=0;
int n=s.nextInt();
if(n%2==0)
flag=prime(n+1);
else
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
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%2f55334861%2ftimeout-for-large-input-in-biginteger%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to remove the recursion and use the for loop to calculate the factorial.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static void factorial(long n, long nn)
BigInteger answer=new BigInteger("1");
BigInteger sum=BigInteger.valueOf(nn);
int foundMatch =0;
for(long i=n;i>0;i--)
answer=answer.multiply(new BigInteger(String.valueOf(i)));
if((answer.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
foundMatch = 1;
break;
if(foundMatch!=1)
System.out.println("NO");
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long nn=n*(n+1)/2;
factorial(n, nn);
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting along
toBigInteger
long after it has overflows (probably multiple times) does not really help. UseBigInteger
from the beginning.
– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
|
show 8 more comments
Try to remove the recursion and use the for loop to calculate the factorial.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static void factorial(long n, long nn)
BigInteger answer=new BigInteger("1");
BigInteger sum=BigInteger.valueOf(nn);
int foundMatch =0;
for(long i=n;i>0;i--)
answer=answer.multiply(new BigInteger(String.valueOf(i)));
if((answer.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
foundMatch = 1;
break;
if(foundMatch!=1)
System.out.println("NO");
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long nn=n*(n+1)/2;
factorial(n, nn);
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting along
toBigInteger
long after it has overflows (probably multiple times) does not really help. UseBigInteger
from the beginning.
– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
|
show 8 more comments
Try to remove the recursion and use the for loop to calculate the factorial.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static void factorial(long n, long nn)
BigInteger answer=new BigInteger("1");
BigInteger sum=BigInteger.valueOf(nn);
int foundMatch =0;
for(long i=n;i>0;i--)
answer=answer.multiply(new BigInteger(String.valueOf(i)));
if((answer.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
foundMatch = 1;
break;
if(foundMatch!=1)
System.out.println("NO");
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long nn=n*(n+1)/2;
factorial(n, nn);
Try to remove the recursion and use the for loop to calculate the factorial.
import java.util.*;
import java.math.BigInteger;
public class TestClass
static void factorial(long n, long nn)
BigInteger answer=new BigInteger("1");
BigInteger sum=BigInteger.valueOf(nn);
int foundMatch =0;
for(long i=n;i>0;i--)
answer=answer.multiply(new BigInteger(String.valueOf(i)));
if((answer.mod(sum)).equals(BigInteger.valueOf(0)))
System.out.println("YES");
foundMatch = 1;
break;
if(foundMatch!=1)
System.out.println("NO");
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
long n=s.nextLong();
long nn=n*(n+1)/2;
factorial(n, nn);
edited Mar 25 at 10:46
answered Mar 25 at 10:01
VSBVSB
2522 silver badges9 bronze badges
2522 silver badges9 bronze badges
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting along
toBigInteger
long after it has overflows (probably multiple times) does not really help. UseBigInteger
from the beginning.
– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
|
show 8 more comments
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting along
toBigInteger
long after it has overflows (probably multiple times) does not really help. UseBigInteger
from the beginning.
– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
Sir, I tried What you told but now it was passing 3 test cases only out of 50.
– Satyanarayanareddy Tadi
Mar 25 at 10:04
1
1
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Do you have to user recursion only ??
– VSB
Mar 25 at 10:12
Converting a
long
to BigInteger
long after it has overflows (probably multiple times) does not really help. Use BigInteger
from the beginning.– tobias_k
Mar 25 at 10:13
Converting a
long
to BigInteger
long after it has overflows (probably multiple times) does not really help. Use BigInteger
from the beginning.– tobias_k
Mar 25 at 10:13
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
Sir, Any method was fine for me . But it was not getting for your answer and even my logic also.
– Satyanarayanareddy Tadi
Mar 25 at 10:15
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
shall i take input in form of biginteger
– Satyanarayanareddy Tadi
Mar 25 at 10:17
|
show 8 more comments
You can use the logic that if the intermediate product of the factorial is divisible by the sum, then the entire factorial is also divisible by sum.
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass
static boolean isFactorialDivisible(int n, BigInteger sum)
BigInteger p = BigInteger.ONE;
for (int i = n; i > 0; i--)
p = p.multiply(BigInteger.valueOf(n));
if (p.mod(sum).equals(BigInteger.ZERO))
return true;
BigInteger gcd = p.gcd(sum);
if (!gcd.equals(BigInteger.ONE))
p = p.divide(gcd);
sum = sum.divide(gcd);
return false;
public static void main(String args[]) throws Exception
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n * (n + 1) / 2;
BigInteger sum = BigInteger.valueOf(nn);
boolean p = isFactorialDivisible(n, sum);
if (p)
System.out.println("YES");
else
System.out.println("NO");
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
add a comment |
You can use the logic that if the intermediate product of the factorial is divisible by the sum, then the entire factorial is also divisible by sum.
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass
static boolean isFactorialDivisible(int n, BigInteger sum)
BigInteger p = BigInteger.ONE;
for (int i = n; i > 0; i--)
p = p.multiply(BigInteger.valueOf(n));
if (p.mod(sum).equals(BigInteger.ZERO))
return true;
BigInteger gcd = p.gcd(sum);
if (!gcd.equals(BigInteger.ONE))
p = p.divide(gcd);
sum = sum.divide(gcd);
return false;
public static void main(String args[]) throws Exception
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n * (n + 1) / 2;
BigInteger sum = BigInteger.valueOf(nn);
boolean p = isFactorialDivisible(n, sum);
if (p)
System.out.println("YES");
else
System.out.println("NO");
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
add a comment |
You can use the logic that if the intermediate product of the factorial is divisible by the sum, then the entire factorial is also divisible by sum.
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass
static boolean isFactorialDivisible(int n, BigInteger sum)
BigInteger p = BigInteger.ONE;
for (int i = n; i > 0; i--)
p = p.multiply(BigInteger.valueOf(n));
if (p.mod(sum).equals(BigInteger.ZERO))
return true;
BigInteger gcd = p.gcd(sum);
if (!gcd.equals(BigInteger.ONE))
p = p.divide(gcd);
sum = sum.divide(gcd);
return false;
public static void main(String args[]) throws Exception
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n * (n + 1) / 2;
BigInteger sum = BigInteger.valueOf(nn);
boolean p = isFactorialDivisible(n, sum);
if (p)
System.out.println("YES");
else
System.out.println("NO");
You can use the logic that if the intermediate product of the factorial is divisible by the sum, then the entire factorial is also divisible by sum.
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass
static boolean isFactorialDivisible(int n, BigInteger sum)
BigInteger p = BigInteger.ONE;
for (int i = n; i > 0; i--)
p = p.multiply(BigInteger.valueOf(n));
if (p.mod(sum).equals(BigInteger.ZERO))
return true;
BigInteger gcd = p.gcd(sum);
if (!gcd.equals(BigInteger.ONE))
p = p.divide(gcd);
sum = sum.divide(gcd);
return false;
public static void main(String args[]) throws Exception
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n * (n + 1) / 2;
BigInteger sum = BigInteger.valueOf(nn);
boolean p = isFactorialDivisible(n, sum);
if (p)
System.out.println("YES");
else
System.out.println("NO");
answered Mar 25 at 10:20
pkgajulapallipkgajulapalli
4906 silver badges20 bronze badges
4906 silver badges20 bronze badges
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
add a comment |
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
Sir, Still time out is coming
– Satyanarayanareddy Tadi
Mar 25 at 10:33
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
No sir, still it was getting time out for 15 test cases
– Satyanarayanareddy Tadi
Mar 25 at 10:41
add a comment |
import java.util.*;
class TestClass
public static int prime(int n)
int count=0,flag=0;
if(n==1)
flag=1;
if(n==2)
flag=0;
else
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
flag=1;
break;
if(flag==1)
return 1;
return 0;
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
int flag=0;
int n=s.nextInt();
if(n%2==0)
flag=prime(n+1);
else
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
add a comment |
import java.util.*;
class TestClass
public static int prime(int n)
int count=0,flag=0;
if(n==1)
flag=1;
if(n==2)
flag=0;
else
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
flag=1;
break;
if(flag==1)
return 1;
return 0;
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
int flag=0;
int n=s.nextInt();
if(n%2==0)
flag=prime(n+1);
else
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
add a comment |
import java.util.*;
class TestClass
public static int prime(int n)
int count=0,flag=0;
if(n==1)
flag=1;
if(n==2)
flag=0;
else
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
flag=1;
break;
if(flag==1)
return 1;
return 0;
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
int flag=0;
int n=s.nextInt();
if(n%2==0)
flag=prime(n+1);
else
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
import java.util.*;
class TestClass
public static int prime(int n)
int count=0,flag=0;
if(n==1)
flag=1;
if(n==2)
flag=0;
else
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
flag=1;
break;
if(flag==1)
return 1;
return 0;
public static void main(String args[] ) throws Exception
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
int flag=0;
int n=s.nextInt();
if(n%2==0)
flag=prime(n+1);
else
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
answered Mar 27 at 22:03
Satyanarayanareddy TadiSatyanarayanareddy Tadi
409 bronze badges
409 bronze badges
add a comment |
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%2f55334861%2ftimeout-for-large-input-in-biginteger%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
1
You might not have to calculate the entire product. After each multiplication, check whether the intermediate product is divisible by the sum; if it is, you can stop, as the final product will also be divisible. This will not help with "negative" cases, but might speed up the "positive" ones considerably.
– tobias_k
Mar 25 at 10:06
2
BTW, what is really meant by "it's elements"? Are you sure that that's "all the numbers from 1 to n"?
– tobias_k
Mar 25 at 10:08
@tobias_k yeah you are right
– Satyanarayanareddy Tadi
Mar 25 at 10:10
Also, you might get an integer overflow while calculating
nn
, better uselong
or also ´BigInteger. What is the upper-bound for
n`, anyway?– tobias_k
Mar 25 at 10:12
1
But that requires a login. I won't sign up just to be able to judge this question for you. Please add the relevant details to your question, and do not tell people to click links, especially not links where they must sign up to access the content.
– Rudy Velthuis
Mar 25 at 17:44