Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Created February 22, 2019 15:57
Show Gist options
  • Save Vaguery/fb098a399218bdae02cb1e4c0b359d23 to your computer and use it in GitHub Desktop.
Save Vaguery/fb098a399218bdae02cb1e4c0b359d23 to your computer and use it in GitHub Desktop.
To practice coding, I want to write a function which, given a strictly positive integer `p`, produces the _smallest_ `q` such that `p*q` is pandigital
require 'pp'
require 'prime'
# for integer p in the given range, find the smallest integer q such that p*q is a pandigital number (has at least one of every digit)
p_range = (1..10)
@digits = (0..9).to_a.collect {|d| d.to_s}
def pandigital?(number)
number.to_s.chars.uniq.sort == @digits
end
def prime_factors(number)
decomposition = number.prime_division
pfactors = []
decomposition.each do |pf|
pfactors = pfactors + [pf[0]]*pf[1]
end
return pfactors
end
def every_binary(length)
[true,false].repeated_permutation(length)
end
def split_array(array,mask)
good = []
bad = []
(0...array.length).each do |idx|
mask[idx] ? (good << array[idx]) : (bad << array[idx])
end
return [good,bad]
end
def product(numbers)
numbers.inject(1,:*)
end
def all_pairs(number)
pf = prime_factors(number)
every_binary(pf.length).collect do |mask|
factors = split_array(pf,mask)
factors.collect {|f| product(f)}.sort
end.uniq.sort
end
@discoveries = {}
["0","2","3","4","5","6","7","8","9"].permutation(9).each_with_index do |p,i|
n = (["1"]+p).join("").to_i
p "#{n}: #{@discoveries.count}" if (i % 10000 == 0)
pairs = all_pairs(n)
pairs.each do |pair|
idx = pair[0]
@discoveries[idx] = pair[1] if @discoveries[idx].nil?
end
end
pp @discoveries.sort
@Vaguery
Copy link
Author

Vaguery commented Feb 22, 2019

This little experimental Ruby version doesn't get all of them. It works through all the 10-digit pandigital numbers starting with 1, and looks for all of the factor pairs of those numbers, and collects them. That means it doesn't get some important-feeling small numbers, like p=100.

@Vaguery
Copy link
Author

Vaguery commented Feb 22, 2019

The output of the thing above looks like this (abbreviated to fit here):

"1023456789: 0"
"1039846725: 18661"
"1059763428: 23321"
"1079623458: 25607"
"1098536724: 27046"
"1239574608: 30409"
"1259403678: 32288"
"1279346805: 33334"
"1298064537: 34120"
"1329045678: 34689"
"1358926704: 35275"
"1378652409: 35759"
"1397602458: 36151"
"1428637905: 36483"
"1458372609: 36861"
"1478302569: 37212"
"1497236805: 37514"
"1528074639: 37788"
"1548023679: 38124"
"1576924803: 38456"
"1596742308: 38770"
"1627803459: 39020"
"1647528903: 39324"
"1675382409: 39617"
"1695302478: 39914"
"1726348905: 40212"
"1746083529: 40544"
"1765023489: 40811"
"1794825603: 41130"
"1825763409: 41430"
"1845702369: 41729"
"1864527903: 41955"
"1894362507: 42224"
"1925403678: 42527"
"1945237806: 42832"
"1964073528: 43112"
"1984023567: 43370"
[[1, 1023456789],
 [2, 511728399],
 [3, 341152263],
 [4, 255864474],
 [5, 204693579],
 [6, 170576133],
 [7, 146208114],
 [8, 127932237],
 [9, 113717421],
 [10, 123456789],
 [11, 93125079],
 [12, 85288158],
 [13, 78727446],
 [14, 73104057],
 [15, 68231193],
 [16, 63966123],
 [17, 60203394],
 [18, 56858711],
 [19, 53866251],
 [20, 61728399],
 [21, 48736038],
 [22, 46562544],
 [23, 44498943],
 [24, 42644079],
 [25, 40938759],
 [26, 39363723],
 [27, 37905807],
 [28, 36552807],
 [29, 35292033],
 [30, 41152263],
 [31, 33014799],
 [32, 31983093],
 [33, 31041693],
 [34, 30101697],
 [35, 29242197],
 [36, 28429386],
 [37, 27666351],
 [38, 26933571],
 [39, 26242482],
 [40, 30864474],
 [41, 24962409],
 [42, 24368019],
 [43, 23801625],
 [44, 23281272],
 [45, 22743731],
 [46, 22249476],
 [47, 21775914],
 [48, 21322041],
 [49, 20887344],
 [50, 24693579],
 [51, 20067798],
 [52, 19681884],
 [53, 19310562],
 [54, 18952907],
 [55, 18661545],
 [56, 18276426],
 [57, 17955417],
 [58, 17646201],
 [59, 17346744],
 [60, 20576133],
 [61, 16778016],
 [62, 16507998],
 [63, 16245346],
 [64, 15991812],
 [65, 15745689],
 [66, 15520848],
 [67, 15275907],
 [68, 15052176],
 [69, 14832981],
 [70, 17636697],
 [71, 14416407],
 [72, 14214693],
 [73, 14020272],
 [74, 13833216],
 [75, 13646253],
 [76, 13467051],
 [77, 13303584],
 [78, 13121241],
 [79, 12955392],
 [80, 15432237],
 [81, 12635269],
 [82, 12481218],
 [83, 12331296],
 [84, 12184269],
 [85, 12040821],
 [86, 11901123],
 [87, 11764011],
 [88, 11640636],
 [89, 11499885],
 [90, 13717421],
 [91, 11246778],
 [92, 11124738],
 [93, 11004933],
 [94, 10887957],
 [95, 10773441],
 [96, 10661031],
 [97, 10555317],
 [98, 10443672],
 [99, 10347231],
 [101, 10135197],
 [102, 10033899],
 [103, 9936666],
 [104, 9840942],
 [105, 9747399],
 [106, 9655281],
 [107, 9565308],
 [108, 9476462],
 [109, 9391275],
 [110, 11225079],
 [111, 9222117],
 [112, 9138213],
 [113, 9057159],
 [114, 8977857],
 [115, 8899893],
 [116, 8823186],
 [117, 8747494],
 [118, 8673372],
 [119, 8600661],
 [120, 10288158],
 [121, 8467578],
 [122, 8389008],
 [123, 8320803],
 [124, 8253999],
 [125, 8187759],
 [126, 8122673],
 [127, 8059761],
 [128, 7995906],
 [129, 7933875],
 [130, 9496683],
 [131, 7812747],
 [132, 7760424],
 [133, 7695306],
 [134, 7640037],
 [135, 7581311],
 [136, 7526088],
 [137, 7471377],
 [138, 7416492],
 [139, 7363953],
 [140, 8818407],
 [141, 7258638],
 [142, 7208433],
 [143, 7164873],
 [144, 7107347],
 [145, 7059861],
 [146, 7010136],
 [147, 6962448],
 [148, 6916608],
 [149, 6869763],
 [150, 8231193],
 [151, 6778125],
 [152, 6734592],
 [153, 6689266],
 [154, 6651792],
 [155, 6605127],
 [156, 6560628],
 [157, 6520698],
 [158, 6477696],
 [159, 6436854],
 [160, 7716123],
 [161, 6357447],
 [162, 6317648],
 [163, 6279012],
 [164, 6240609],
 [165, 6220515],
 [166, 6165648],
 [167, 6131592],
 [168, 6092142],
 [169, 6057684],
 [170, 7262217],
 [171, 5985139],
 [172, 5951133],
 [173, 5921379],
 [174, 5882067],
 [175, 5848497],
 [176, 5820318],
 [177, 5782248],
 [178, 5749992],
 [179, 5717853],
 [180, 6858711],
 [181, 5655069],
 [182, 5623389],
 [183, 5592672],
 [184, 5562369],
 [185, 5533767],
 [186, 5502666],
 [187, 5483358],
 [188, 5444037],
 [189, 5415121],
 [190, 6497784],
 [191, 5358978],
 [192, 5330604],
 [193, 5303619],
 [194, 5280237],
 [195, 5248563],
 [196, 5221836],
 [197, 5199948],
 [198, 5173616],
 [199, 5143014],
 [201, 5091969],
 [202, 5072148],
 [203, 5041719],
 [204, 5017392],
 [205, 4992579],
 [206, 4968333],
 [207, 4944327],
 [208, 4920471],
 [209, 4901373],
 [210, 5878899],
 [211, 4851018],
 [212, 4829229],
 [213, 4805469],
 [214, 4782654],
 [215, 4760325],
 [216, 4738231],
 [217, 4717782],
 [218, 4697136],
 [219, 4673424],
 [220, 5612544],
 [221, 4631085],
 [222, 4611072],
 [223, 4589595],
 [224, 4569552],
 [225, 4548751],
 [226, 4529898],
 [227, 4508748],
 [228, 4489017],
 [229, 4469823],
 [230, 5367726],
 [231, 4434528],
 [232, 4411593],
 [233, 4392612],
 [234, 4373747],
 [235, 4356117],
 [236, 4336686],
 [237, 4318464],
 [238, 4301973],
 [239, 4282623],
 [240, 5144079],
 [241, 4246875],
 [242, 4233789],
 [243, 4211769],
 [244, 4194504],
 [245, 4179051],
 [246, 4160406],
 [247, 4143627],
 [248, 4128822],
 [249, 4110432],
 [250, 4938759],
 [251, 4078719],
 [252, 4061423],
 [253, 4049739],
 [254, 4029912],
 [255, 4013607],
 [256, 3997953],
 [257, 3983112],
 [258, 3967041],
 [259, 3953493],
 [260, 4748373],
 [261, 3921337],
 [262, 3907998],
 [263, 3891546],
 [264, 3880212],
 [265, 3862143],
 [266, 3847653],
 [267, 3833295],
 [268, 3824082],
 [269, 3805092],
 [270, 4572474],
 [271, 3776715],
 [272, 3763044],
 [273, 3748926],
 [274, 3735729],
 [275, 3732309],
 [276, 3708246],
 [277, 3694797],
 [278, 3682323],
 [279, 3668311],
 [280, 4409856],
 [281, 3642237],
 [282, 3629319],
 [283, 3616848],
 [284, 3604221],
 [285, 3591147],
 [286, 3582783],
 [287, 3567195],
 [288, 3553677],
 [289, 3541383],
 [290, 4257162],
 [291, 3518439],
 [292, 3505068],
 [293, 3493773],
 [294, 3481224],
 [295, 3469383],
 [296, 3458304],
 [297, 3449077],
 [298, 3435426],
 [299, 3427002],
 [301, 3401946],
 [302, 3389238],
 [303, 3378399],
 [304, 3367296],
 [305, 3355695],
 [306, 3344633],
 [307, 3333798],
 [308, 3325896],
 [309, 3312222],
 [310, 3983409],
 [311, 3301767],
 [312, 3280314],
 [313, 3270222],
 [314, 3260349],
 [315, 3249133],
 [316, 3238848],
 [317, 3228858],
 [318, 3218427],
 [319, 3211272],
 [320, 3858093],
 [321, 3188436],
 [322, 3178809],
 [323, 3168603],
 [324, 3158824],
 [325, 3149199],
 [326, 3139506],
 [327, 3130425],
 [328, 3124206],
 [329, 3111993],
 [330, 3741693],
 [331, 3095406],
 [332, 3082824],
 [333, 3074039],
 [334, 3065796],
 [335, 3055185],
 [336, 3046071],
 [337, 3038175],
 [338, 3028842],
...
 [44442, 44458],
 [44443, 44568],
 [44448, 44637],
 [44454, 44706],
 [44457, 44622],
 [44463, 44661],
 [44468, 44604],
 [44469, 44485],
 [44472, 44607],
 [44473, 44658],
 [44481, 44523],
 [44487, 44666],
 [44490, 44676],
 [44496, 44619],
 [44499, 44580],
 [44510, 44613],
 [44514, 44625],
 [44522, 44532],
 [44523, 44589],
 [44529, 44583],
 [44538, 44562],
 [44544, 44571],
 [44558, 44604],
 [44568, 44574],
 [44576, 44577]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment