Hacker Rank : Project Euler#1 - c

I am new to competitive programming and I did a problem on Hacker Rank. The question statement is as follows:
"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Input Format
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output Format
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N."
Constraints
1≤T≤10^5
1≤N≤10^9
I've written the following code which successfully satisfies 3 test cases and fails at remaining two.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int func(int p,int n)
{
int j;
n=n-1;
j=n/p;
return (p*j*(j+1))/2;
}
int main()
{
unsigned long int n;
int t,j,count;
scanf("%d",&t);
if(t>=1 && t<=100000){
for(j=0;j<t;j++)
{
scanf("%lu",&n);
if(n>=1 && n<=1000000000)
{
count=func(3,n)+func(5,n)-func(15,n);
printf("%d\n",count);
}
}}
return 0;
}
What is the mistake in my code. Why isn't it getting accepted?

There are a couple of issues.
You are indeed overflowing your int when returning from func. Also, your printf statement should be printf("%llu\n", count);
So, the return from func, count, and the local variable j, should all be unsigned long long and your print out should reflect that as well. You need to make j unsigned long long because of the arithmetic in the return statement for func(this is at least the case in VS 2013).

Related

Sum of odd numbers from 1 - 100 using RECURSION in C

Trying to figure out where I am going wrong in this code, I realize I keep getting 1 because that's what am I passing in the function but how else can I do this?
#include <stdio.h>
#include <stdlib.h>
int totalOdd();
int main(){
printf("%d\n",totalOdd(1));
}
int totalOdd(int n){
int odd = n;
if(odd >= 100){
return 0;
}
else{
totalOdd(odd+2);
}
return odd;
}
try this one
one :
#include <stdio.h>
#include <stdlib.h>
int totalOdd(int);
int main(){
printf("%d\n",totalOdd(1));
}
int totalOdd(int n)
{
int odd = n;
if(odd > 100){
return 0;
}
else{
return (n+totalOdd(odd+2));
}
}
in your code , addition was missing
#include <stdio.h>
#include <stdlib.h>
int totalOdd();
int main(){
printf("%d\n",totalOdd(1));
}
int totalOdd(int odd){
if(odd >= 100)
return 0;
return (odd + totalOdd(odd + 2));
}
Not a complete answer, because this sounds like homework, but here’s an example of how to write a very similar function, first recursively, and then a more efficient tail-recursive solution.
#include <stdio.h>
#include <stdlib.h>
unsigned long factorial1(const unsigned long n)
{
/* The naive implementation. */
if ( n <= 1U )
return 1; // 0! is the nullary product, 1.
else
return n*factorial1(n-1);
/* Notice that there is one more operation after the call to
* factorial1() above: a multiplication. Most compilers need to keep
* all the intermediate results on the stack and do all the multiplic-
* ations after factorial1(1) returns.
*/
}
static unsigned long factorial_helper( const unsigned long n,
const unsigned long accumulator )
{
/* Most compilers should be able to optimize this tail-recursive version
* into faster code.
*/
if ( n <= 1U )
return accumulator;
else
return factorial_helper( n-1, n*accumulator );
/* Notice that the return value is simply another call to the same function.
* This pattern is called tail-recursion, and is as efficient as iterative
* code (like a for loop).
*/
}
unsigned long factorial2(const unsigned long n)
{
return factorial_helper( n, 1U );
}
int main(void)
{
printf( "%lu = %lu\n", factorial1(10), factorial2(10) );
return EXIT_SUCCESS;
}
Examining the output of both gcc -O -S and clang -O -S on the above code, I see that in practice, clang 3.8.1 can compile both versions to the same optimized loop, and gcc 6.2.0 does not optimize for tail recursion on either, but there are compilers where it would make a difference.
For future reference, you wouldn’t solve this specific problem this way in the real world, but you will use this pattern for other things, especially in functional programming. There is a closed-form solution to the sum of odd numbers in a range. You can use that to get the answer in constant time. You want to look for those whenever possible! Hint: it is the sum, from i = 0 to 100, of 2 i + 1. Do you remember a closed-form formula for the sum of i from 0 to N? 0, 1, 3, 6, 10, 15, ...? The proof is often taught as an example of a proof by induction. And what happens to a sum from 0 to N when you multiply and add by constants?
As for my example, when I have had to compute a factorial function in a real program, it was for the purpose of computing a probability distribution (specifically, the Poisson distribution) for a simulation, and I needed to calculate the factorial of the same numbers repeatedly. Therefore, what I did was store a list of all the factorials I’d already calculated, and look up any number I saw again in that list. That pattern is called memoization.

For Loop condition

#include <stdio.h>
#define all_mem (sizeof(x) /sizeof(x[0]))
int x[] ={1,2,3,4,5,6,7,8,9,10};
int main(void) {
// printf("The condition is %d",(all_mem-2));
int i;
for(i=-1;i<=(all_mem-2);i++)
{
printf("The number is %d",i);
}
return 0;
}
In the above code for loop is not even executing for a single time, i tried printing condition and its satisfies for loop condition. Any insights how macro expression in for loop condition is evaluated to the value less than -1?
The all_mem macro is returning a size_t value; integer promotion rules mean the comparison of i <= (all_mem - 2) is promoting i to a size_t, which means the value is huge, rather than -1. Try casting to ensure signed comparison:
for(i = -1; i <=(ssize_t)(all_mem- 2); ++i)
after making a few corrections to the code, forinstance, sizeof() returns a size_t, not an int the code worked perfectly.
Here is the modified code:
#include <stdio.h>
#define all_mem (sizeof(x) /sizeof(x[0]))
int x[] ={1,2,3,4,5,6,7,8,9,10};
int main(void)
{
printf( "all_mem = %ld\n", all_mem ); // << added for debugging
// printf("The condition is %d",(all_mem-2));
int i;
for(i=-1;i<=(int)(all_mem-2);i++) // <<
// << properly cast to int
// otherwise compiler raises warning and unsigned comparison made
{
printf("The number is %d\n",i);
}
return 0;
}
Here is the output from the above code:
all_mem = 10
The number is -1
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
When compiling, always enable all the warnings, then fix those warnings.
If the above statement had been followed, then you would have seen the problem, without our help.
(for gcc, at a minimum use: -Wall -Wextra -pedantic and I also add: -Wconversion -std=c99)

power int number while in loop

OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.

why < is much faster than !=?

Problem : Consider the following algorithm to generate a sequence of
numbers. Start with an integer n. If n is even, divide by 2. If n is
odd, multiply by 3 and add 1. Repeat this process with the new value
of n, terminating when n = 1. The input will consist of a series of
pairs of integers i and j, one pair of integers perline. All integers
will be less than 1,000,000 and greater than 0.
For each pair of
input integers i and j, output i, j in the same order in which they
appeared in the input and then the maximum cycle length for integers
between and including i and j. These three numbers should be separated
by one space, with all three numbers on one line and with one line of
output for each line of input.
sample input :
1 10
sample output:
1 10 20
so i wrote this :
#include <stdio.h>
#include <string.h>
struct line{int in1;int in2;int result;};
int cycle(int in);
int main(int argc, char *argv[]) {
int cycle(int in);
char c;
int firstIn=0;
struct line l[500] ;
int pointer=0;
while(2<3){
l[pointer].in1=0;
l[pointer].in2=0;
scanf("%u %u",&l[pointer].in1,&l[pointer].in2);
if(l[pointer].in1<1||l[pointer].in2<1){
break;
}
int maxCyc=0;
int j,m;
int min,max;
if(l[pointer].in1>l[pointer].in2){
max=l[pointer].in1;
min=l[pointer].in2;
}
else{
max=l[pointer].in2;
min=l[pointer].in1;
}
for(j=min;j<=max;j++){
m = cycle(j);
if(m>maxCyc)
maxCyc=m;
}
l[pointer].result=maxCyc;
printf("%d %d %d\n",l[pointer].in1,l[pointer].in2,l[pointer].result);
pointer++;
}
}
int cycle(int in){
int cyc = 1;
while(in>1){
if(in%2==0){
cyc++;
in=in/2;
}
else{
cyc++;
in=in*3+1;
}
}
return cyc;
}
Its completly ok but when you change while(in>1) in cycle method to while(in!=1) it gets much more slower. my question is why?!
Time when its while(in>1) : 0.683 sec
and when its while(in!=1) : I waited more than 5 min nothing
happened yet :)
for input : 1 1000000
there is no infinite loop or something because in cant get below 1 at all(for that it must be already 1) .
Best regards
When you call cycle with the input value 113383, the process eventually sets n to
827370449, and 3*827370449+1 is 2482111348, which is greater than the maximum signed int and is interpreted as -1812855948. So there's your first negative number where there should be no negative number.
If this process then eventually sets n to -2, it will loop infinitely between -2 and -1 from then on. There may be other loops I haven't considered.
If you were to use an unsigned int, there is a possibility (I haven't checked) that this too will overflow eventually, which will not result in a negative value but will result in an incorrect value, invalidating your results.
No matter what integer representation you use, it would probably be a good idea to compare n with (maximum-1)/3 at the top of each loop, where maximum is the largest possible positive value of your integer type, just to be sure you do not overflow.
As you told me it was a simple overflow problem thx everyone.
max int value is 2,147,483,647; So when i changed int cycle(int in) to int cycle(long long int in) my problem was solved.
i also figured it out that my first answer with while(in>1) was wrong.
When an integer overflow occurs,the value will go below 0 .That was the reason while(in!=1) was an infinte loop.
I was really tired that i didn't figure it out by myself. sorry for that :)

0-1 sequences without repeating 1's

My task is to write a program, where the input is an exponent of 2, and the output is the number of sequences (out of the maximum 2^n sequences) where there are no 1's next to each other. (n<=50)
For example, on the input of 3, the output is 5, because 2^3=8 and out of the 8 possibilities the only acceptable ones are: (000, 001, 010, 100, 101) and (110,011,111) are not acceptable because there are 2 or more 1's next to each other.
My program works fine until 31, upon the number 32 it stops working, overflow issues I guess. I tried long int and unsigned int, none of those seemed to help.
#include <stdio.h>
#include <math.h>
main(){
int t,i,n,j,ki;
scanf("%d",&t);
for (i=1;i<=t;i++){
scanf("%d",&n);
ki=pow(2,n)-(n*(n-1))/2;
printf("Scenario #%d:\n%d\n\n",i,ki);
}
return 0;
}
Help me pl0x.
For variable ki:
Step #1: Use unsigned long long instead of int.
Step #2: Use 1<<n instead of pow(2,n).
Step #3: Use llu% instead of %d.
int main()
{
int t,i,n;
unsigned long long ki,one=1;
scanf("%d",&t);
for (i=1;i<=t;i++)
{
scanf("%d",&n);
ki = (one<<n)-n*(n-1)/2;
printf("Scenario #%d:\n%llu\n\n",i,ki);
}
return 0;
}

Resources