How to generate a fix length 14 digit long random number. I am using postman and lodash lib but somehow sometimes it works sometimes it doesn't
pm.globals.set('randomNumer_SO',_.random(1, 100000000000000));
This sometimes generate 13 digit number too. But I always want a 14 digit as else API fails.
When you use _.random(1, 100000000000000) you ask for a number between 1 (length 1), and 100000000000000 (length 15). Most of the time, you'll get numbers with length 14, but you might also get numbers of length 1, 2, 3, etc...
To get a number with fixed length of 14, set the lower and upper bounds to the minimal and maximal numbers with that length:
_.random(10000000000000, 99999999999999)
Related
I'm working on c++ and I need help writing a program that prints all numbers between 1 and 200 excluding multiples of 7. I understand how to print the number between 1 and 200, but I don't understand how to print them excluding multiples of 7.
Use the modulo operator % to determine if the number is a multiple of 7.
if number % 7 == 0, then it's a multiple of seven, and then you don't print it.
You can pre calculate the multiples of 7, storing them somewhere.
Then, for every value in the 'for' you print only if the number is not in where you stored the pre calculations.
I know what I want but I have no idea if there's a technical name for this or how to go about calculating it.
Suppose I have a string:
ABCDEFGHI
This string can be split evenly into a "MAXIMUM" of 3 characters each sub-string.
Basically, I'm trying to find out how to split a string evenly into its maximum sub-lengths. A string of 25 characters can be evenly split into 5 parts consisting of 5 characters each. If I tried to split it into 4 or 6 pieces, I'd have an odd length string (a string with a size different from the others).
A string of 9 characters can be split into only 3 pieces of 3 characters each.
A string of 10 characters can be split into only 2 pieces of 5 characters each.
A string of 25 characters can be split into only 5 pieces of 5 characters each.
A string of 15 characters can be split into 3 pieces of 5 characters each OR 5 pieces of 3 characters each.
A string of 11 characters cannot be split because one string will always be larger than the other.
So how do I go about doing this? I've thought of using the square root but that doesn't work for a string of "10" characters. Works for 9 and 25 just fine.
Any ideas? Is there a technical name for such a thing? I thought of "Greatest Common Divisor", but I'm not so sure.
Well... let me see... I think that if I got it right, you want to verify if a certain number (the length of your string) is prime or not :)
A first idea would be this:
1) get length of string, make a loop where you divide the length of the string by all numbers from 2 up to (length of string/2) [you will need to check if this (length of string/2) is a whole number too, and adjust it if not ;)]
2) if at least ONE number divides it, bam. (You check this one by verifying if there is a remainder after the division)
3) if not, you got yourself a prime. Sorry, no even division.
Of course that approach would not be very fast for very long strings... just an idea.
It is only about prime number and composite number, a basic math concept. The algorithm you need is Primality Test
I need to generate numbers in between a specific range and after 1st cycle if number doesn't suit, I need to delete it from the range.
Example:
I am going to generate random number from range [1-55]. I want number
24 but generated through random generator. After first cycle random
generator has generate number 4. In next cycle i want to generate
random number from same range but number 4 is excluded. So range is -
[1,54] / {4}. If in next cycle was number 28 generated the range will
change to - [1,54] /{4,28}. Cycles will repeat until the number 24 is
not generated.
I need it for my evolutionary algorithm. If i use big ranges it takes so long to generate the needed number through random number generation algorithm. I will be grateful for any advice.
Start with an array containing the full range:
int range[55];
std::iota(range, range+55, 1);
Start with n = 55.
Now, at each step:
select a random number i from 0 to n-1
swap range[i] with range[n-1].
decrement n
If the value at the index you selected was 24, then you're done. The values selected prior to 24 are at the right hand side of your array, and the remaining unselected values are at the left.
I'm assuming of course that you need to know those values. If all you need is to generate the value 24 then int generate24() { return 24;} should do it. If all you need to know is how many steps it takes to generate 24, then that's uniformly distributed on the range 1 to 55, so you can probably fake it with a single random number generation.
Outside the binary 01010, octal 01122, decimal integer 1234 and hexadecimal 0xFF, does any one have any idea or trick how to build a new number format? For example, 0x11AEGH has it's range from 0 - 9 to A - H. I'm going to build password generator, so it would be very helpful if anyone can put something on it that might help.
Firstly, Is there any function which can do this? Basically I want to convert 0x11AEGH to binary, octal, integer and so on...
Formatting a number in an N-ary system requires two things: an alphabet, and an ability to obtain results of integer division + the remainder.
Consider formatting a number in a base-26 system using the Latin alphabet. Repeatedly obtain the remainder R of division by 26, pick letter number R, and add it to the front of the number that you are formatting. Integer-divide the number by 26, and use it in the next step of the algorithm. Stop when you reach zero.
For example, if you print 1234 in base-26, you can do it like this:
1234 % 26 is 12. Add M; 1234/26 is 47
47 % 26 is 21. Add V; 47 / 26 is 1
1 % 26 is 1. Add B. 1 / 26 is zero; stop.
So 1234 in base-26 is BVM.
To convert back, start from the front, and sequentially subtract the designated "zero" (A in case of the above example) from each digit, like this:
B-A is 1. Result is 1
V-A is 21. Result is 1*26+21, which is 47
M-A is 12. Result is 47*26+12, which is 1234.
You can probably use functions like Long.parseLong(String representation, int radix) and Long.toString(long value, int radix) as they are in Java. Other languages may also have the similar features.
This approach restricts the maximal possible radix (base) and you have no control over the characters representing different digits (alphanumeric chars are used). However it is a ready solution without any extra coding.
An emirp (prime spelled backwards) is a pime number whose reversal is also prime. Ex. 17 & 71. I have to write a program that displays the first 100 emirps. It has to display 10 numbers per line and align the numbers properly:
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157.
I have no cue what I am doing and would love if anyone could dump this down for me.
It sounds like there are two general problems:
Finding the emirps.
Formatting the output as required.
Break down your tasks into smaller parts, and then you'll be able to see more clearly how to get the whole task done.
To find the emirps, first write some helper functions:
is_prime() to determine whether a number is prime or not
reverse_digits() to reverse the digits of any number
Combining these two functions, you can imagine a loop that finds all the numbers that are primes both forward and reversed. Your first task is complete when you can simply generate a list of those numbers, printing them to the console one per line.
Next, work out what format you want to use (it looks like a fixed format of some number of character spaces per number is what you need). You know that you have 100 numbers, 10 per line, so working out how to format the numbers should be straightforward.
Break the problem down into simpler sub-problems:
Firstly, you need to check whether a number is prime. This is such a common task that you can easily Google it - or try a naive implementation yourself, which may be better given that this is homework.
Secondly, you need to reverse the digits of a number. I'd suggest you figure out an algorithm for this on a piece of paper first, then implement it in code.
Put the two together - it shouldn't be that hard.
Format the results properly. Printing 10 numbers per line is something you should be able to figure out easily once the rest is done.
Once you have a simple version working you might be able to optimise it in some way.
A straight forward way of checking if a number is prime is by trying all known primes less than it and seeing if it divides evenly into that number.
Example: To find the first couple of primes
Start off with the number 2, it is prime because the only divisors are itself and 1, meaning the only way to multiple two numbers to get 2 is 2 x 1. Likewise for 3.
So that starts us off with two known primes 2 and 3. To check the next number we can check if 4 modulo 2 equals 0. This means when divide 2 into 4 there is no remainder, which means 2 is a factor of 4. Specifically we know 2 x 2 = 4. Thus 4 is not prime.
Moving on to the next number: 5. To check five we try 5 modulo 2 and 5 modulo 3, both of which equals one. So 5 is prime, add it to our list of known primes and then continue on checking the next number. This rather tedious process is great for a computer.
So on and so forth - check the next number by looping through all previous found primes and check if they divide evenly, if all previously found primes don't divide evenly, you have a new prime. Repeat.
You can speed this up by counting by 2's, since all even numbers are divisible by two. Also, another nice trick is you don't have to check any primes greater than the square root of the number, since anything larger would need a smaller prime factor. Cuts your loops in half.
So that is your algorithm to generate a large list of primes.
Collect a good chunk of them in an array, say the first 10,000 or so. And then loop through them, reverse the numbers and see if the result is in your array. If so you have a emirp, continue until you get the first 100 emirps
If the first 10,000 primes don't return 100 emirps. Move on to the next 10,000. Repeat.
For homework, I would use a fairly simplistic isPrime function, pseudo-code along the lines of:
def isPrime (num):
set testDiv1 to 2
while testDiv1 multiplied by testDiv1 is less than or equal to num:
testDiv2 = integer part of (num divided by testDiv1)
if testDiv1 multiplied by testDiv2 is equal to num:
return true
Add 1 to testDiv1
return false
This basically checks whether the number is evenly divisible by any number between 2 and the square root of the number, a primitive primality check. The reson you stop at the square root is because you would have already found a match below it if there was one above it.
For example 100 is 2 times 50, 4 times 25, 5 time 20 and 10 times 10. The next one after that would be 20 times 5 but you don't need to check 20 since it would have been found when you checked 5. Any positive number can be expressed as a product of two other positive numbers, one below the square root and one above (other than the exact square root case of course).
The next tricky bit is the reversal of digits. C has some nice features which will make this easier for you, the pseudo-code is basically:
def reverseDigits (num):
set newNum to zero
while num is not equal to zero:
multiply newnum by ten
add (num modulo ten) to newnum
set num to the integer part of (num divided by ten)
return newNum
In C, you can use int() for integer parts and % for the modulo operator (what's left over when you divide something by something else - like 47 % 10 is 7, 9 % 4 is 1, 1000 % 10 is 0 and so on).
The isEmirp will be a fairly simplistic:
def isEmirp (num):
if not isPrime (num):
return false
num2 = reverseDigits (num)
if not isPrime (num2):
return false
return true
Then at the top level, your code will look something like:
def mainProg:
create array of twenty emirps
set currEmirp to zero
set tryNum to two
while currEmirp is less than twenty
if isEmirp (tryNum):
put tryNum into emirps array at position currEmirp
add 1 to currEmirp
for currEmirp ranging from 0 to 9:
print formatted emirps array at position currEmirp
print new line
for currEmirp ranging from 10 to 19:
print formatted emirps array at position currEmirp
print new line
Right, you should be able to get some usable code out of that, I hope. If you have any questions of the translation, leave a comment and I'll provide pointers for you, rather than solving it or doing the actual work.
You'll learn a great deal more if you try yourself, even if you have a lot of trouble initially.