Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: Counting command or script (Read 1739 times) previous topic - next topic
0 Members and 4 Guests are viewing this topic.

Re: Counting command or script

Reply #30
 
Then it means you are a novice reaching monk level pretty soon. Just look what beauty 'I have in store' for bumping up your Perl skills 😛

Tried to perl this myself but, hey I'm not even a perl novice 😄

So let's perl this I'll add a print screen. Of course take your time I know this must be challenging. If you can't  or don't have the time I'll let it here so hopefully other guys can give it a shot. There is no hurry, and of course with many thanks and gratitude 🤩 in advance



What the perl script steps has to do to solve the above formula is in the following order:

 1 Prompt user to input T  (what are the total elements, can be 94 or whatever or even bitcoin word list 2048)
 2 Prompt user to input S  (desired pwd length has to be   smaller than T so you can't feed a T=10 and an S=15)
 3 Prompt user input n (the number of twos he needs to check if is better or worse to have more or less twos in his pwd for the provided T and S))
 4 Determine Z with Z=(S-n) (both S and n are provided by the user in the above steps)
 5 Determine k with K=2*n   (k is twice as bigger than n)

 6 Proceed solve for  Λ that is the whole formula I'll reproduce it bellow one more time
 
      Λ = T!/(T-Z)!*S!/[(2^n)*n!*(S-k)!]

Added an example for verifying  the script if gives the proper result. Took an example like bellow:

when T=13 and S=7 and n=3 from user input we will have a Z=4 and  k=6

The result should be 17.160 * 105 = 1.801.800


Re: Counting command or script

Reply #31
Code: [Select]
#!/usr/bin/perl
use strict;
no strict 'refs';
use warnings;
use 5.36.0;

die "Usage: $0 <T: total elements> <S: pass length> <n: number of twos>" if $#ARGV < 2;

our ($T, $S, $n) = (int $ARGV[0], int $ARGV[1], int $ARGV[2]);

for (qw/T S n/) {
    die "$$_ not a positive integer" if !($$_ > 0);
}
die "Not T>S" if ($S > $T);
die "Not n<S" if ($n > $S);
die "Not S-n<T" if ($S-$n > $T);
die "Not 2n<S" if (2*$n > $S);

my $Z=$S-$n;
my $k=2*$n;
#        T! /    (T-Z)! *     S! / [(2^n) *   n!   *  (S-k)! ]
say fact($T)/fact($T-$Z)*fact($S)/((2**$n)*fact($n)*fact($S-$k));

sub fact {
    my $f = shift;
    my $x = 1;
    $x *= $_ foreach ($x++ .. $f);
    return $x;
}
Code: [Select]
% ./wtf.pl
Usage: ./wtf.pl <T: total elements> <S: pass length> <n: number of twos> at ./p.pl line 7.
% ./wtf.pl 13 7 3
900900
It gives half of what you said, one of us is mistaken somewhere.

Re: Counting command or script

Reply #32
 
Hmmm let me check it out, 1 minute please.

 Redone the calculation and gives me the same result 1.801.800. Now i'm looking into you script perhaps it can have some division by 2 somewhere

My best bet I can think of when looking at the script is that instead of dividing by 2^3 which is 8 it divides (2^3)*2=16  so I guess that's why gives half the result cos divides by 16 instead of just 8

 Here could  be the error (2**$n), just a guess though

The calc is this   13!/9!   ×  7!/2^3*6  = 17160 × 105 = 1.801.800

Also all number a integer natural number. There's no rational number anywhere in the calculation.

Saw the second term from the script gives 52.5  instead of 105 cos divides 105 by 2 so that's why result is half what it should

EDIT 3: Quadrupled-triple checked everything should work fine, no error on my end, its perl fault. Perl secretly hates math 😋


Re: Counting command or script

Reply #33
The error was in the fact sub, duh.
Code: [Select]
#!/usr/bin/perl
use strict;
no strict 'refs';
use warnings;
use 5.36.0;

die "Usage: $0 <T: total elements> <S: pass length> <n: number of twos>" if $#ARGV < 2;

our ($T, $S, $n) = (int $ARGV[0], int $ARGV[1], int $ARGV[2]);

for (qw/T S n/) {
    die "$$_ not a positive integer" if !($$_ > 0);
}
die "Not T>S" if ($S > $T);
die "Not n<S" if ($n > $S);
die "Not S-n<T" if ($S-$n > $T);
die "Not 2n<S" if (2*$n > $S);

my $Z=$S-$n;
my $k=2*$n;
#        T! /    (T-Z)! *     S! / [(2^n) *   n!   *  (S-k)! ]
say fact($T)/fact($T-$Z)*fact($S)/((2**$n)*fact($n)*fact($S-$k));

sub fact {
    return 0 if (my $f = shift) == 0;
    my ($i, $p) = (1, 1);
    $p *= $_ foreach ($i++ .. $f);
    return $p;
}
From now on you're on your own.  ;)

Re: Counting command or script

Reply #34
 Wow now works 1000%, amazing!!! I'll  retract, perl likes math 😀 👍🏻 but you have to be a super-blackhat-hacker. It works the only thing it doesn't do is the moment you input n=0 meaning no rep at all. But I'm ultra-gut with this 5 out of 5⭐ solution 💯. It's more than I could think of, wow 🍾 ✅ 🤩 🤩 🤩

I'll figure out that n=0

Many thanks @nous  #1 🌞 ☮


Re: Counting command or script

Reply #35
 
 Haha, made it in python. It may look dumb (i'm n00b) but works even when n=0. Also works when use T = 2048 bitcoin word list. Saw the perl script throws NaN in that case
Code: [Select]
# Online Python - IDE, Editor, Compiler, Interpreter
T = int(input("T: "))
S = int(input("S: "))
n = int(input("n: "))
Z = int(S)-int(n)
k = 2*int(n)
import math
a = math.factorial(T)
b = math.factorial(T-Z)
c = math.factorial(S)
d = math.factorial(n)*2**n*math.factorial(S-k)
e = int(a)*int(c)
f = int(b)*int(d)
g = (e/f)
print(int(g))

Re: Counting command or script

Reply #36
Code: [Select]
'''Permutations of n elements drawn from k values'''

from itertools import product


# replicateM :: Applicative m => Int -> m a -> m [a]
def replicateM(n):
    '''A functor collecting values accumulated by
       n repetitions of m. (List instance only here).
    '''
    def rep(m):
        def go(x):
            return [[]] if 1 > x else (
                liftA2List(lambda a, b: [a] + b)(m)(go(x - 1))
            )
        return go(n)
    return lambda m: rep(m)


# TEST ----------------------------------------------------
# main :: IO ()
def main():
    '''Permutations of two elements, drawn from three values'''
    print(
        fTable(main.__doc__ + ':\n')(repr)(showList)(

            replicateM(7)

        )(['abcdefg'])
    )


# GENERIC FUNCTIONS ---------------------------------------

# liftA2List :: (a -> b -> c) -> [a] -> [b] -> [c]
def liftA2List(f):
    '''The binary operator f lifted to a function over two
       lists. f applied to each pair of arguments in the
       cartesian product of xs and ys.
    '''
    return lambda xs: lambda ys: [
        f(*xy) for xy in product(xs, ys)
    ]


# DISPLAY -------------------------------------------------

# fTable :: String -> (a -> String) ->
#                     (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
    '''Heading -> x display function -> fx display function ->
                     f -> xs -> tabular string.
    '''
    def go(xShow, fxShow, f, xs):
        ys = [xShow(x) for x in xs]
        w = max(map(len, ys))
        return s + '\n' + '\n'.join(map(
            lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
            xs, ys
        ))
    return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
        xShow, fxShow, f, xs
    )


# showList :: [a] -> String
def showList(xs):
    '''Stringification of a list.'''
    return '[' + ','.join(
        showList(x) if isinstance(x, list) else repr(x) for x in xs
    ) + ']'


# MAIN ---
if __name__ == '__main__':
    main()

Hello it's me again  :D . I need to put some pauses when the above python script runs, I'm trying a 10x10 string which may drain too much resources  if run it without pause (10^10x10 bytes strings) Found bellow script that looks like it's good so I can ctr+c and pause the script then run it again pressing enter. Looks like it has a problem
1 idk where/how to insert it in my main py
2 the pause script looks like has some error print (...)

Also would b cool if somehow I can make the script dump results on disk and not keep it in memory like it seems to be doing till the end

Bellow the pause script found on stack

Code: [Select]
import time

while True:
    try:
        time.sleep(30)  # do something here
        print '.',

    except KeyboardInterrupt:
        print '\nPausing...  (Hit ENTER to continue, type quit to exit.)'
        try:
            response = raw_input()
            if response == 'quit':
                break
            print 'Resuming...'
        except KeyboardInterrupt:
            print 'Resuming...'
            continue

Thank you !!! 😇  No hurry 😇