Compatible Numbers
January 5, 2016
Two numbers are compatible if they have the same prime factors, though with possibly different multiplicities.
Your task is to write a program to determine if two numbers are compatible. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Learning a new language (Perl6) – so thought I’d try and solve it in that … syntactically it is a bit terse
Notes:
* $a??$b!!$c is perl6’s ternary operator
* $a%%$b returns true if $a is a multiple of $b
* first returns the first entry for which the condition is true o/w returns an object of type “Nil” which is an empty list
* WHICH returns the type of the object…
In Python.
January 5th, 2016.cpp:
The program is known to run on an Apple Power Mac G4 (AGP Graphics) (450MHz processor, 1GB memory) on both Mac OS 9.2.2 (International English) (the program compiled using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition)) and Mac OS X 10.4.11 (the program compiled using Xcode 2.2.1).
The program is able to – in addition to what is specified in the description of the problem – try to determine the least integer “compatible” with, but not equal to, the integer entered by the user; however, the approach the program takes to do that – beginning with 2 testing if every integer ≤
UINT_MAX
(besides the integer entered by the user) is “compatible” with the integer entered by the user until such an integer is found – is needlessly slow; a faster approach to take would be to decompose the integer entered by the user into its prime factors and then:if the multiplicity of every of those prime factors is 1, set the multiplicity of the least of those prime factors to 2;
or, if the multiplicity of any of those prime factors is not 1, set the multiplicity of every of those prime factors to 1.
(I’ve just completed a gig at London South Bank University and so I’m again just trying to solve the problems posed by this ‘site whilst I try to get a job (and I’ve solved this problem in particular to test my
seal_List
template class I might use in the fourth version of my SDL2 joystick interrogator utility); I’m well-aware that my solutions are far from the best – but, in my defence, I don’t have any traditional qualifications in computer science :/ )