Wrong answer given by a julia code for project euler 29 - julia

I was trying to solve Project Euler Q.29 in julia and I am getting a wring answer.
Here is the problem statement:
Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
2^2=4, 2^3=8, 2^4=16, 2^5=32
3^2=9, 3^3=27, 3^4=81, 3^5=243
4^2=16, 4^3=64, 4^4=256, 4^5=1024
5^2=25, 5^3=125, 5^4=625, 5^5=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
Here is my code, its pure brute force.
powers = []
n = 100
for a in collect(2:n)
for b in collect(2:n)
append!(powers,a^b)
end
end
length(unique(powers))
The correct answer is 9183 but I am getting 6143 as the answer.
Can someone please help me with this?

You're suffering from integer overflow - see the manual here:
julia> 2^62
4611686018427387904
julia> 2^63
-9223372036854775808
You can overcome the issue by using BigIntegers like so:
powers = []
n = 100
for a in collect(2:n)
for b in collect(2:n)
# note the `big()` here
append!(powers,big(a)^b)
end
end
length(unique(powers))

Related

how many n-length binary sequence problem

how to find out the solution to this problem in python/java or any other language:
Thanks in advance
Since a program isn't a proof and you would still need to prove it, here is some Python code:
def zig_zag(seq):
"""tests if binary sequence seq satsifies zig-zag pattern"""
for i in range(len(seq)-1):
if (i%2 == 0 and seq[i] > seq[i+1]) or (i%2 == 1 and seq[i] < seq[i+1]):
return False
return True
def count_zig_zags(n):
"""counts the number of binary zig-zag patterns of length n"""
count = 0
for i in range(2**n):
b = bin(i)[2:]
if zig_zag(b): count += 1
return count
For example:
>>> [count_zig_zags(n) for n in range(1,12)]
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
A proof would be via strong induction.

How to reverse integer in Prolog using tail-recursion?

I would like to make a predicat reverse(N,Result) in Prolog.
For example:
reverse(12345,Result).
Result = 54321.
I have to use tail-recursion. I can use *, +, - and divmod/4 and that's all.I can't use list.
I can reverse a number < 100 but I don't find how to finish my code, I can't complete my code to reverse integers bigger than 100 correctly.
reverse(N,N):-
N <10,
N>0.
reverse(N,Result):-
N > 9,
iter(N,0,Result).
iter(N,Ac,Result):-
N < 100, !,
divmod(N,10,Q,R),
R1 is R*10,
Result is Q + R1.
Can I have some help please ?
Thanks you in advance.
I suggest the use of CLP(FD), since it offers declarative reasoning over integer arithmetic and a lot of Prolog systems provide it. Concerning the digit-reversal, I recommend you take a look at entry A004086 in The On-Line Encyclopedia of Integer Sequences. In the paragraph headed FORMULA, you'll find, among others, the following formulae:
a(n) = d(n,0) with d(n,r) = if n=0 then r else d(floor(n/10),r*10+(n mod 10))
These can be translated into a predicates by adding an additional argument for the reversed number. First let's give it a nice declarative name, say digits_reversed/2. Then the relation can be expressed using #>/2, #=/2, (/)/2, +/2, mod/2 and tail-recursion:
:- use_module(library(clpfd)).
digits_reversed(N,X) :-
digits_reversed_(N,X,0).
digits_reversed_(0,R,R).
digits_reversed_(N,X,R) :-
N #> 0,
N0 #= N/10,
R1 #= R*10 + (N mod 10),
digits_reversed_(N0,X,R1).
Note that digits_reversed/2 correspond to a(n) and digits_reversed_/3 corresponds to d(n,r) in the above formulae. Now let's query the predicate with the example from your post:
?- digits_reversed(12345,R).
R = 54321 ;
false.
The predicate can also be used in the other direction, that is ask What number has been reversed to obtain 54321? However, since leading zeros of numbers are omitted one reversed number has infinitely many original numbers:
?- digits_reversed(N,54321).
N = 12345 ;
N = 123450 ;
N = 1234500 ;
N = 12345000 ;
N = 123450000 ;
N = 1234500000 ;
N = 12345000000 ;
N = 123450000000 ;
.
.
.
Even the most general query yields solutions but you'll get residual goals as an answer for numbers with more than one digit:
?- digits_reversed(N,R).
N = R, R = 0 ; % <- zero
N = R,
R in 1..9 ; % <- other one-digit numbers
N in 10..99, % <- numbers with two digits
N mod 10#=_G3123,
N/10#=_G3135,
_G3123 in 0..9,
_G3123*10#=_G3159,
_G3159 in 0..90,
_G3159+_G3135#=R,
_G3135 in 1..9,
R in 1..99 ;
N in 100..999, % <- numbers with three digits
N mod 10#=_G4782,
N/10#=_G4794,
_G4782 in 0..9,
_G4782*10#=_G4818,
_G4818 in 0..90,
_G4818+_G4845#=_G4842,
_G4845 in 0..9,
_G4794 mod 10#=_G4845,
_G4794 in 10..99,
_G4794/10#=_G4890,
_G4890 in 1..9,
_G4916+_G4890#=R,
_G4916 in 0..990,
_G4842*10#=_G4916,
_G4842 in 0..99,
R in 1..999 ;
.
.
.
To get actual numbers with the query above, you have to restrict the range of N and label it after the predicate has posted the arithmetic constraints:
?- N in 10..20, digits_reversed(N,R), label([N]).
N = 10,
R = 1 ;
N = R, R = 11 ;
N = 12,
R = 21 ;
N = 13,
R = 31 ;
N = 14,
R = 41 ;
N = 15,
R = 51 ;
N = 16,
R = 61 ;
N = 17,
R = 71 ;
N = 18,
R = 81 ;
N = 19,
R = 91 ;
N = 20,
R = 2 ;
false.
If for some reason you don't want a constraints based solution, or if you using a Prolog system not supporting constraints, an alternative solution is:
reverse_digits(N, M) :-
( integer(N) ->
reverse_digits(N, 0, M)
; integer(M),
reverse_digits(M, 0, N)
).
reverse_digits(0, M, M) :- !.
reverse_digits(N, M0, M) :-
N > 0,
R is N div 10,
M1 is M0 * 10 + N mod 10,
reverse_digits(R, M1, M).
This solution can be used with either argument bound to an integer and leaves no spurious choice-points:
?- reverse_digits(12345, M).
M = 54321.
?- reverse_digits(N, 12345).
N = 54321.
?- reverse_digits(12345, 54321).
true.
But note that this solution, unlike the constraints based solution, cannot be used as a generator of pairs of integers that satisfy the relation:
?- reverse_digits(N, M).
false.
reverseNumber(N,R):-reverse_acc(N,0,R).
reverse_acc(0,Acc,Acc).
reverse_acc(N,Acc,R):- C is N mod 10, N1 is N div 10,
Acc1 is Acc * 10 + C,
reverse_acc(N1, Acc1,R).

Find all the factors of a number from the prime factors

Using Elliptic curve factorization (in Python) I am able to find the PRIME factors of a 50 digit number in ~0.5 sec. Is there any way I can convert the prime factors to the factors of the number?
What I have realized by testing on small digits (496 and 28), multiplying the prime factors together in a specific order. Then multiplying those numbers almost gives me the factors, but it is not very flexible because I have only gotten the formula of what I need to multiply together from small list of prime factors (1,2,3,5).
Here's my version, which computes the element-wise products of the powerset of the set of prime factors, keeping only those products that are unique:
def divisors(n, fs=[]):
if fs == []: fs = factors(n)
divs = [1]
for f in fs:
temp = divs[:]
for d in divs:
if f * d not in temp:
temp.append(f*d)
divs = temp
return sorted(divs)
And here it is in action:
>>> factors(496)
[2, 2, 2, 2, 31]
>>> divisors(496)
[1, 2, 4, 8, 16, 31, 62, 124, 248, 496]
>>> factors(28)
[2, 2, 7]
>>> divisors(28)
[1, 2, 4, 7, 14, 28]
If the number is factored into powers of primes, like p^a q^b r^c then the possible factors of the number are all numbers of the form p^x q^y r^z for 0 ≤ x ≤ a, 0 ≤ y ≤ b, and 0 ≤ r ≤ z.
Since you can have different numbers of prime factors, this is a little programming problem. Have fun.
Maybe you're looking for the product of each (unique) set in the powerset. So for 18=2*3*3 you want the product for each set in {{},{2},{3},{3},{2,3},{2,3},{3,3},{2,3,3}}, giving you {1, 2, 3, 3, 6, 9, 18}.
You can use sympy module like this:
import sympy
sympy.ntheory.factorint(4960) #Factorization.
sympy.ntheory.divisors(4960) #List all divisors.

Moving modulus operator to the other side of the equation

I have a mathematical problem that is part of my programming problem
I have a statement like
a = b%30;
How can I calculate b in terms of a?
I gave it a thought but couldn't figure it out.
By definition,
b == 30*n + a
for some integer n.
Note that there are multiple b values that can give you the same a:
>>> b = 31
>>> b % 30
1
>>> b = 61
>>> b % 30
1
First, obviously, there are in general several solutions for b for a given a.
If % is the remainder operator found in most programming languages, then the sign of a is critical. You know that this is a website for programming questions, right?
If |a|>=30, then there are no solutions
If a = 0, the solutions are the multiples of 30.
If 0 < a < 30, the solutions are all the b such that b = 30 * k + a for some positive integer k.
If -30 < a < 0, the solutions are all the b such that b = - (30 * k - a) for some positive integer k.

Does RSA encrpytion work for small numbers?

Suppose:
p = 3
q = 11
n = 33
phi = 20
e = 7
d = 3
If I want to encrypt the number 123, I would do (123^7) % 33, which equals18.
Now if I want to decrypt 18, I do (18^3) % 33, which gives me 24.
As you can see, the input number and decrypted number is not the same. Does anyone know why this is? Also does this mean I have to break the number 123 up into single digits and then encrypt 1, 2 and 3 separately?
EDIT: I am aware that due to the value of n, anything I mod by n would be lower than n. Does that mean I have to intially choose very large numbers for p and q?
From the Wikipedia page for RSA (my emphasis):
Bob then wishes to send message M to Alice.
He first turns M into an integer m, such that 0 <= m < n by using an
agreed-upon reversible protocol known as a padding scheme. He then
computes the ciphertext c corresponding to
c = m^e (mod n)
Your m (123) is not less than n (33), so it doesn't work. So yes, you would need to start with larger p and q to get a larger n.

Resources