BigNumber.h Arduino how to parse a number larger then 10 digits - c++

i have made a simple bit of code to test a number to see if it is a prime number or not but while feeding it large prime numbers to test the speed of the program on the arduino it whould only take a number at length 9-/under-digits i tested my read function and it returns the entire number but the 'BigNumber' wont parse it insted it just says its 0
code:
void Speed(String num)
{
Serial.println("NUM="+num);
BigNumber NUM = num.c_str();//this is where it fails
BigNumber Curr = "1";//start 2 / 'curr++' start of loop
num = "";
... the testing of prime numbers here
the code stops the arduino if i put a 10 digit number in, the output is so
<|S 1234567891
>|NUM=1234567891
and if i put a number with 9 digits it outputs as expected
<S 123456789
>|NUM=123456789
>|123456789 is not a prime number
>|because ist a factor of 3
i have tryed seeing if anyone has had the same problem as me but i cant find it anywhere.
im use an arduino-uno
EDIT: after doing some more testing it now doesnt set the number insted of crashing after testing 'S 1111111111' (10 digits) its output is normal:
<|S 1111111111
>|NUM=1111111111
>|1111111111 is not a prime number
>|because ist a factor of 11
but if i put in 11 digits it parses as 0 ??
<|S 11111111111
>|NUM=11111111111
>|0 cant be a prime number because it doesn't end in 1,3,7,9
bty: i forgot to mention that 'S number_here' S specifys the mothod of finding the result i also have D=DataCrunch it checks all the numbers and L=List witch creates a list of found Prime numbers like a prime number search, and thay work fine exept that DataCrunch (D) has the same problem with parsing the number given.
EDIT2:
this is proof that BigNumber can hold such a large number
https://forum.arduino.cc/index.php?topic=85692.0
in the first post.

so as it turns out after some extensiv research that BigNumber is not fit for very large numbers but another part of the 'BigNumber.h' lib does its bc_num.
bc_num x;
bc_str2num(&x, "9898989898", 10);
String c = "Controll=";
c+=bc_num2str(x);
Serial.println(c);
output
Controll=9898989898
but as you can see this takes a bit more programming to get implamented and so im going to go off and start now bye.

Related

File size of 1 million rand numbers

The file by rand is 1 million random numbers. It is compressed down to 415 kb....how is this possible if it is impossible to compress random data.
Thank you.
Jon Hutton
You're most likely talking about the famous "A Million Random Digits" test data that was published in 1955. So it's digits, not numbers, as Mark already guessed, that's why the binary version is only 415,241 bytes. Also see Mark Nelson's homepage that has a link to the binary file.
Note that the end result (the binary file) is not compressible without knowing it - although there are some small redundancies in the file that come from the way it was created - see this forum entry for more details:
There are potentially other biases in the million random digits file
that I discussed years ago in comp.compression. The data was
originally generated by sampling a 5 bit counter driven by a noisy
oscillator to produce a set of 20,000 punched cards with 50 digits
each. But there was some correlation between consecutive digits, so
what they did was add adjacent pairs of cards modulo 10 to produce a
new set of cards which was published. That is why the sums of the
columns are even. Each of the original cards is counted twice.
Sounds like they're stored as one decimal digit per byte. So using only ten of the 256 possible bytes values leaves you with the potential for a log(256)/log(10) compression ratio on random digits, which is about 2.4. You're getting 2.35 (assuming "kb" = 1024 bytes). Voila.
You can get 2.4 quite easily by coding every three digits into ten bits, since 1024 > 1000. Then you can code 1,000,000 decimal digits into 416,667 bytes, or 406.9 KiB.
With a little more difficulty, using something like GMP, you could code it as a giant million-digit integer in binary, which would take 415,242 bytes, or 405.5 KiB. That would be as good as it gets for random decimal digits.

While loop excluding multiples of specific number

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.

Calculate max even lengths of a string to be split

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

Project Euler # 51 c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to solve problem 51 in project euler.
Here is the problem statement : Project Euler Problem 51
I tried the approach given below.
Generate all the prime numbers between 2 and 10 power 8 using Sieve of Eratosthenes
Gather all n-digit numbers from sieve (i.e .. first i will get all 3 digits, then 4 digits .. so on and compute on it)
check if the number has repeated digit (as per problem statement we have 2 *s of same digit) . If yes , convert into string
if yes, find the position where it repeats and save it as a key (for ex: 12341 will result 04 as key since 1 is the number which is repeated in 0 and 4th position)
now based on the key , insert into the bucket (Bucket is a multimap which contains key as repeated position (04) and value as the prime number)
For each key in bucket, delete the repeated position . If am taking 04 bucket key , then all the prime numbers in that bucket would have the strings repeated in the positions 0 and 4. Am deleting 0th and 4th position of the string which would result my number (12341) as 234 and insert it into a map , which will store the occurence as (234, 8 being the trimmed number and number of occurences).
Now, if the key 234 is repeated 8 times, find the numbers which got trimmed and resulted 234. Those are the answers.
I ran this algorithm in c++ , and for 7 primes 56003, 56113, 56333, 56443, 56663, 56773, and 56993 is resulting less than a second ..
but , for 8 digits , i crossed 10 power 8 primes and still it didnt result any answer. I believe answer is above that limit.
And when i try to generate primes between 2 and 10 power 9 its aborting , since i am storing all the numbers in vector.
My question is ,
Is there any way by which we can fine tune the above mentioned steps and find the answer,
or i need to think some other way to find the answer .
Note: Just for an example i took 12341.
There is atleast one issue in your brute force solution. You are assuming exactly 2 digits are * but question never mentions this. There may be 1 or 3 or more digits which when replaced with the same digits 0-9 still generate primes.
It is impossible to have 8 prime with 1 or 2 * for the following reason:
If you use just 1 *, and let's say replace it with 1 to get a prime(let's call this prime p). Now if p % 3 = 1, you can not replace * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 2 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with one * impossible for any number of digits.
If you use just 2 *, and let's say replace both with 1 to get a prime(let's call this prime p). Now if p % 3 = 2, you can not replace both * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 1 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with two * impossible for any number of digits.
This is the reason why your code does not give the required output. You should perhaps try with 3 * characters.

Prime backwards in order

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.