Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today I came across a question. It says that:
You need to find if a number can be expressed as sum of two perfect powers. That is, given x find if there exists non negative integers a, b, m, n such that a^m + b^n = x where 1 <= x <= 1000000 and m > 1, n > 1
Could someone please explain me how this can be done?
I know that we can write something like this:
for(int a = 1; true; a++){
for(int b = 1; true; b++){
// And so on and so forth
}
}
But this is not the very efficient (or correct) way of doing so.
Thanks.
Sometimes, that is the only way to solve these kind of problems. The one you have posted belongs to a class of problems called "one way functions": there is a trivial implementation of the problem in one way...
Given four non negative integers, a, b, m and n, find x so a^m + b^n = x
But the other way...
Given x, find four non negative integers a, b, m and n, so a^m + b^n = x
is non trivial. In fact, it could be impossible to solve it, or your best chance, have to use a force brute algorithm to solve it, which is what you have proposed.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Good evening. I had a coding interview on Codesignal with the question below, and I got just 14/20 with the test cases. How will you solve it please.
Given an array of nums, add positive and negative in succession and return the sum.
Example : given nums = {2, 3, 4, 5, 7}
Answer = 2-3+4-5+7 = 5.
What's the fastest algorithm for this?
I tried to use a two for loops and input -ve with i+1 for the second loop, but that's just brute force and terribly slow
There is no way to avoid an exhaustive accumulation of the values, but this is terribly... fast. There is no need and no way to accelerate, besides unrolling the loop (which will probably have little effect) and parallelizing (out of the scope of the question).
IMO, the most reasonable way is with
int sum= 0; int i;
for (i= 0; i + 1 < n; i+= 2)
{
sum+= num[i] - num[i+1];
}
if (i < n)
{
sum+= num[i];
}
If the data is int32, positive, and the processor 64 bits, a nasty hack would be to load two values at a time as 64 bits and accumulate this way. In the end, you split back to 32 bits and perform the final subtraction. But again, I doubt the this will yield a significant speedup.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I don't get what I should put in the place of: "α" and "β" in order to print all the 3 digit numbers which are different from 0 and from one another.
It is required to replace those 2 variables, with some code
We are entering G(0)!!!
It's from an exam paper, I really don't get it, please help.
void G(int k)
{int i;
for(i=1;i<=α;i++)
{ p[k]=i;
if(β)G(k+1);
else
printf("%d%d%d\n",p[0],p[1],p[2]);
}
}
For any of this to make sense, it must be that p is declared as a global int array of dimension at least 3. I assume for the purposes of this answer that it is in fact so declared.
Note that the function sets p[k] = i, but it later reads back only p[0], p[1], and p[2]. This should give you a pretty good idea about what makes sense for expression β, which controls whether to recurse (increasing k) or print.
Note also that the function sets p[k] = i, and that when it reads back those p[k] for various k, it wants to get values ranging from 1 to 9 (no more and no less). This should give you a pretty good idea of what expression makes sense for α, the inclusive upper bound on i.
Having figured those out, it remains to satisfy yourself that the natural substitutions for those expressions indeed produce a resulting function that behaves as required when initially called as G(0). I suspect that you will find that easier than you did discerning the needed expressions.
(Details are left as an exercise.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having trouble on thinking of away to attack this problem.
X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First write the algorithm and then implement the code in C.
x= 1/2+1/3+...1/n+1 answer: n = 83
The only thing I'm sure of is that it uses a for loop.At first I was thinking something like
For(int i = 0; i <= n.....
That doesn't seem close though.I dunno..Can I get a hint on where to start?
You will obviously compute the partial sums X.n and stop when X.n<4 and X.n+1>4.
To compute the partial sums, keep an accumulator variable and add the fractions one after the other
n= 0
S= 0
// Repeat the following instructions
n+= 1
S+= 1/(n+1) // Now, S = X.n
Remains to find the stopping condition. As the value of S goes increasing from 0, we will stop as soon as S exceeds 4. In other words, continue as long as S remains below 4.
n= 0
S= 0
while S < 4
n+= 1
S+= 1/(n+1) // Now, S = X.n
Translate that to C syntax.
Remains to look closer at the possibility that X.n = 4.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
if the range of d is 1<= d <= 10^101 and n is 1<= n <= 200. since the range of double is 2.3E-308 to 1.7E+308. when i take input 11111111111111111111 as d then the value d become 11111111111111111000.000 when i show the value to terminal. that means that it couldn't take the input correctly then how will it give correct value for 10^101. i need know the nth root of d. d will be always in form of p = k^n. that's why i added pow function to know the nth root. but the problem is that the range of p is huge. what i am trying is to solve this problem Power of Cryptography !
int main(){
double d,n;
scanf("%lf%lf", &n, &d))
{
printf("%lf\n", pow(d, 1/n));
}
return 0;
}
A double precision number is not capable of holding all the values between 2.3E-308 to 1.7E+308, it is capable of holding a value between these numbers to a precision of about 15 decimal places.
That means some numbers (such as your example) require more precision than the 8 bytes of data can store.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Basically i've attempted to answer this question from Kattis:
https://open.kattis.com/problems/mixedfractions
I have a working solution however it goes over the allocated 2 second time limit for computation. My question is essentially, is it even possible to write a solution to this problem in the C programming language that can compute the maximum input in under 2 seconds.
When reading through the other statistics "problems/mixedfractions/statistics" I've noticed that nobody else has used C or similar procedural languages. The feedback just says it went over the time limit but I'm not sure if that's because C can't do the calculation fast enough or if I've made a mistake in my code.
Thanks for the help, I'll chuck my code in below for reference!
http://pastebin.com/7EtA2d3S
EDIT: Thanks for the response to the question, as you can see I'm new to the SO scene and to C (well programming in general) and was just trying to practice, sorry if my code was dreadful. Basically to clarify all I was trying to ask was if this particular question is possible in C (because nobody else had done it in C), thanks again and I'll try and get this post removed so not to waste anyone else's time.
I can't imagine how your code remotely managed to exceed 2s runtime on the three samples given.
The version I just knocked up runs so fast it doesn't register any time at all:
#include <stdio.h>
int main(int argc, char *argv[])
{
while (1) {
unsigned int a, b;
unsigned int c, d, e;
scanf("%u %u", &a, &b);
if (a == 0 && b == 0) break;
c = a / b;
d = a % b;
e = b;
printf("%u %u / %u\n", c, d, e);
}
}
Running:
% /usr/bin/time ./mixed < data.in > data.out
0.00 real 0.00 user 0.00 sys
EDIT Ah, I see - Kattis has its own runtime environment, and supplies larger data sets than the sample shown on the page. My time on Kattis itself was 0.04s - not fantastic, but not horrendous either. Using the div(n, d) function that calculates the remainder and quotient in one step is faster.