How do I find max and min values that can be represented with
a 5-digit number that is in base 13 assuming only positive integers
are represented? then the answer needs to be in base 10.
does 5 digit number mean 5bits? Isn't the smallest number that
can be represented a zero and largest is 2^(N-1)?
This sounds like homework, but I'll bite anyway :)
5 digits probably means 5 digits, as in 12345.
Base 13 means there are 13 possible digits where we as humans are used to calculate with 10.
We could represent the extra 3 digits with A, B, C so that the full range of possible digits is 0123456789ABC. With this representation, it's clear that the smallest 5-digit value is 00000 and the largest CCCCC.
To convert CCCCC in base 13 to base 10 you do
((((C * 13) + C) * 13 + C) * 13 + C) * 13 + C
=
((((12 * 13) + 12 ) * 13 + 12 ) * 13 + 12 ) * 13 + 12
=
371,292
00000 is of course zero in any base.
Related
I'm trying to solve programming question, a term called "FiPrima". The "FiPrima" number is the sum of prime numbers before, until the intended prime tribe.
INPUT FORMAT
The first line is an integer number n. Then followed by an integer number x for n times.
OUTPUT FORMAT
Output n number of rows. Each row must contain the xth "FiPrima" number of each line.
INPUT EXAMPLE
5
1 2 3 4 5
OUTPUT EXAMPLE
2
5
10
17
28
EXPLANATION
The first 5 prime numbers in order are 2, 3, 5, 7 and 13.
So:
The 1st FiPrima number is 2 (2)
The 2nd FiPrima number is 5 (2 + 3)
The 3rd FiPrima number is 10 (2 + 3 + 5)
The 4th FiPrima number is 17 (2 + 3 + 5 + 7)
The 5th FiPrima number is 28 (2 + 3 + 5 + 7 + 13)
CONSTRAINTS
1 ≤ n ≤ 100
1 ≤ x ≤ 100
Can anyone create the code ?
I have a program in which I have to generate all r-digit numbers (if r is 2 - all 2-digit numbers) among n digits (these ought to be all numbers from 1 to n inclusive). My question is how can I do this recursively or iteratively, for example if n = 3 and r = 2, the result should be 12 13 21 23 31 32.
I have question that asks how values such as c are computed in terms of binary numbers. Im researching it but now but figured id ask here if anyone has somewhere they can send me or explain how this works.
int main()
{
int a 10, int b = 12, int c, int d;
int c = a << 2; //output 40
}
Well, I'm not answering with C++ code, as the question is not really related to the language.
The integer ten is written 10 in base 10 as it's equal to 1 * 10^1 + 0 * 10^0.
Binary is base 2, so let's try to write ten as a sum of powers of 2.
10 = 8 + 2
That is 2^3 + 2^1.
Let's switch to binary (using only two digits : 0 and 1).
2^3 is written 1000
2^1 is written 10
Their sum is 1010 in binary.
"<<" is the operation that shift left binary digits by a certain amount (beware of overflow).
So 1010 << 2 is 101000
That is in decimal 2^5 + 2^3 = 32 + 8 = 40
You can also think of "<< N" as being a multiplication by 2^N of an integer.
My challenge is to output the total number of five-digit numbers that have a digit 5, but no digits 8. My only two answers so far have been 0, or 90000. Can anyone help me?
#include <iostream>
using namespace std;
int main() {
int number;
int counter=10000;
int ncounter=0;
while (counter >= 10000 && counter <= 99999) {
int n1,n2,n3,n4,n5;
counter = counter + 1;
n1 = number%10;
number /= 10;
n2 = number%10;
number /= 10;
n3 = number%10;
number /= 10;
n4 = number%10;
number /=10;
n5 = number%10;
number /= 10;
if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5)
if (n1!=8)
if (n2!=8)
if (n3!=8)
if(n4!=8)
if (n5!=8)
ncounter=ncounter+1;
}
cout<<ncounter<<endl;
return 0;
}
(num with 5 but not 8) = (num without 8) - (num with neither 8 nor 5) = 8*9*9*9*9 - 7*8*8*8*8= 23816
Each number is a selection of 5 digits (with repetitions).
Since you cannot select the digit 8, you have 9 possible digits, so this problem is equivalent to the same problem, base 9 (instead of base 10).
If you make 1 digit a 5, there are 4 non-5 and non-8 digits remaining. The number of these can be calculated as 8^4 (because there are 8 available digits to choose from, and you need to choose 4 of these). With a single 5, there are 5 ways to position the 5, so multiply by 5.
Similarly with 2 5's, there are 10 ways to position the 5s relative to other digits.
Therefore, we have the following table:
number of digits==5 remaining digits ways to position 5s
1 8^4 5
2 8^3 10 = 5*4/2
3 8^2 10
4 8^1 5
5 8^0 1
There are 5*8^4 + 10*8^3 + 10*8^2 + 5*8^1 + 8^0 = 26281 numbers <10^5 with a 5 but not an 8.
There are 4*8^3 + 6*8^2 + 4*8^1 + 8^0 = 2465 numbers <10^4 with a 5 but not an 8. Therefore, there are 23816 numbers satisfying your criteria.
This is actually a mathematical problem. Here we have three conditions:
First digit is not zero, as it should be a five-digit number.
No digits are 8
One or more digits are 5
There can be numbers with one to five 5s, where first digit is or is not 5 (except for 55555). That's nine cases to count.
If first digit is not 5, it has 7 options: [1234679]; if any other digit is not 5, it has 8 options: [12346790].
Here C(5) is number of combinations to place 5s, and C(o) - to place other digits.
N(5). 1st? C(5) C(o)
1 Y 1 * 8^4
1 N 4 * 7*8^3
2 Y 4 * 8^3
2 N 6 * 7*8^2
3 Y 6 * 8^2
3 N 4 * 7*8
4 Y 4 * 8
4 N 1 * 7
5 Y 1
Sum: 23816
How can I generate in SAS and ID code with 5 digits(letters & Numbers)? Where the first 3 must be letters and last 2 must be numbers.
You can create a unique mapping of the integers from 0 to 26^3 * 10^2 - 1 to a string of the format AAA00. This wikipedia page introduces the concept of different numerical bases quite well.
Your map would look something like this
value = 100 * (X * 26^2 + Y * 26^1 + Z * 26^0) + a * 10^1 + b * 10^0
where X, Y & Z are integers between 0 and 25 (which can be represented as the letters of the alphabet), and a & b are integers between 0 and 9.
As an example:
47416 = 100 * (0 * 26^2 + 18 * 26^1 + 6 * 26^0) + 1 * 10^1 + 6 * 10^0
Using:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You get:
47416 -> [0] [18] [6] (1) (6)
A S G 1 6
So 47416 can be represented as ASG16.
To do this programatically you will need to step through your number splitting it into quotient and remainder through division by your bases (10 and 26), storing the remainder as part of your output and using the quotient for the next iteration.
you will probably want to use these functions:
mod() Modulo function to get the remainder from division
floor() Flooring function which returns the rounded down integer part of a real numer
A couple of similar (but slightly simpler) examples to get you started can be found here.
Have a go, and if you get stuck post a new question. You will probably get the best response from SO if you provide a detailed question, code showing your progress, a description of where and why you are stuck, any errors or warnings you are getting and some sample data.