Problem 7: 2 to N
Time limit: 2 secs
Print the numbers from 2 upto a given number, x.
Input :
First line of input contains number of test cases, t.
Next t lines contain each a number, x.
Output :
For each input case, print the output on a new line. Each line contains the numbers from 2 to x separated by a space.
Constraints :
t<15
1<x<100
Sample Input :
2
4
5
Sample Output:
2 3 4
2 3 4 5
Scoring:
USE OF FUNCTIONS IN "math.h" IS NOT ALLOWED
C: Number of non-whitespace characters
D: Number of digits
S: Number of single quotes
B: Number of bitwise operators ( ~, |, &, ^, >>, << )
I: Number of Increment(++) and Decrement(--) operators
F: Number of times "scanf" is used
S = 12000/(C + 6000*D + 300*S + 200*B + 200*I + 200*F)
Solution:
Possible approaches to solve this problem would be:
-
Run a for/while loop from 2 to n like below:
for(i=2;i<n;i++) printf("%d ",i); printf("n\n");It requires the use of digits and hence result score is really low.
-
By using characters as in 'c'-'a' which is equal to 2, digits can be replaced. Again this approach scores low as use of single quotes have been constrained
- When a main function is defined as main(i,j,k){}, i is initialized to 1.So, to obtain tha value of 2, we need to multiply 'i' by 2 as in just leftshift the bits by 1 as i<<i or by using increment operator as i++ or decrement operator as -(--(- i)), or by simply using i+i. Using bitwise and increment or decrement operators will result in low scores, but the use of i+i will result in a high score
-
There is another concept which can be used here to get 1. The function 'scanf' returns the number of arguments entered. Hence, the statement x=scanf("%d",&t) will give x=1. x=2 can be done in the same way as in the 3rd point.
-
Another approach to solve the problem would require the use of 'enum' to get the values as enum v{a,b} takes the value of a as 0 and b as 1. The value of 2 can be obtained either by using enum v{a,b,c} or use enum v{a,b} and using 'b' to get the value '2' by any of the approaches in third point.
References: A.8.4 Enumerations, Dennis Ritchie
Code:
Implementation of 5th approach:
main(x,t,z,i)
{
enum v{a,b};
gets(&x);
t = atoi(&x);
while(t)
{
t=t-b;
gets(&x);
z = atoi(&x);
for(i=b+b;i<=z;i+=b)
(i!=z)?printf("%d ",i):printf("%d\n",i);
}
}
Check the Best Solutions
Submit your solution
You need to be logged in to submit a solution.
discussions
| maddikolkata | Machine version |
| Q: | What version of machine are being used? Is it 64 bit or 32 bit? Because sizeof() will produce different result.. |
| A: | 64 bit |
| cmds | Keywords |
| Q: | Keywords has influence on score except C (C: Number of non-whitespace characters)? |
| A: | keywords will be counted in C |
| dolphinigle | Trailing space |
| Q: | is trailing space after the answers allowed? |
| A: | No |
post a question
Loading...



