Wednesday, July 7, 2021

Solution via C++ code for #25 from 16052021 Eugene Djobs's stream

Пусть M – сумма делителей, максимально приближенных к квадратному корню числа, не считая единицы и самого числа. Квадратный корень в качестве делителя не рассматривать. Если нет делителей, отличных oт квадратного корня, единицы и самого числа, значение M считается равным 0. Напишите программу, которая перебирает целые числа, большие 710017, в порядке возрастания и ищет среди них такие, для которых значение M кратно 10 и больше M для предыдущего найденного числа.. Вывести первые 5 найденных чисел и соответствующие им значения M.

Замена 710017 на 713000218 и 5 на 100

Python code for original task

[boris@fedora33server FORWARD]$ cat dba25160521.py

numbers = []

M=0

for i in range(713000218, 10000000000):

    minDiv = 0

    for div in range(int(i**0.5),1,-1):

        if i % div == 0 and div**2 != i:

            minDiv = div

            break

    if minDiv != 0:

        currMV = (i // minDiv) + minDiv

    if currMV % 10 == 0 and currMV > M:

        numbers.append(i)

        print(i,currMV)

        M = currMV

    if len(numbers) == 100:

        break

Проверяем что для первых 5 найденных чисел программы дают те же результаты

[boris@fedora33server FORWARD]$ python dba25160521.py

713000221 67330

713000261 437190

713000269 11688590

713000279 23000040

713000571 237666860

[boris@fedora33server FORWARD]$ ./mainVector1

 Input start : 713000218

 Input depth : 5

713000221 67330 

713000261 437190 

713000269 11688590 

713000279 23000040 

713000571 237666860 


***************************
Теперь заменим 5 на 100
***************************

[boris@fedora33server FORWARD]$ cat procVector1.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

std::vector<std::vector<int> >  resultList(int m, int depth)

{

   std::vector<std::vector<int> > vec;

   std::vector<int> v1;

    int i,j,M,minDiv,currMV,div;

    M = 0;

    for (int i = m; i < 10000000000 ; i++) {

        minDiv = 0;    

        for (div = (int)sqrt(i); div > 1; div--)

        {

          if (i%div == 0 && div*div != i)

             {

              minDiv = div ;

              break ;

             }

         }

        if (minDiv != 0)

           currMV = (i/minDiv) + minDiv;

        if (currMV % 10 == 0 && currMV > M)

         {

           v1.push_back(i);

           v1.push_back(currMV);

           vec.push_back(v1);

           M = currMV;

           while (!v1.empty())

           {    

             v1.pop_back();

           }  

          }

        if (vec.size() == depth)

              break;

     }

 return vec;

}

[boris@fedora33server FORWARD]$ cat mainVector1.cpp

#include <iostream>

#include <vector>

#include <cstddef>

std::vector<std::vector<int> > resultList(int m,int n);

int main()

{

   int m,n;

   std::cout << " Input start : "; 

   std::cin >> m ; 

   std::cout << " Input depth : ";

   std::cin >> n ;

   std::vector<std::vector<int> > rez = resultList(m,n);

   // Displaying the 2D vector

    for (int i = 0; i < rez.size(); i++) {

       for (int j = 0; j < rez[i].size(); j++)

           std::cout << rez[i][j] << " ";

     std::cout << std::endl;

  }

}

Compilation via g++ on Fedora Linux 34

$ g++ procVector1.cpp  mainVector1.cpp -o mainVector1

[boris@fedora33server FORWARD]$ ./mainVector1
 Input start : 713000218
 Input depth : 100
713000221 67330 
713000261 437190 
713000269 11688590 
713000279 23000040 
713000571 237666860 
713001381 237667130 
713001441 237667150 
713001621 237667210 
713001651 237667220 
713001711 237667240 
713001831 237667280 
713001921 237667310 
713002011 237667340 
713002071 237667360 
713002161 237667390 
713002431 237667480 
713002611 237667540 
713002701 237667570 
713002881 237667630 
713003091 237667700 
713003241 237667750 
713003271 237667760 
713003451 237667820 
713003721 237667910 
713003901 237667970 
713004141 237668050 
713004231 237668080 
713004261 237668090 
713004351 237668120 
713004441 237668150 
713004771 237668260 
713005071 237668360 
713005311 237668440 
713005401 237668470 
713005701 237668570 
713005791 237668600 
713005941 237668650 
713006421 237668810 
713006661 237668890 
713006751 237668920 
713006961 237668990 
713007111 237669040 
713007231 237669080 
713007411 237669140 
713007651 237669220 
713007951 237669320 
713008221 237669410 
713008371 237669460 
713008641 237669550 
713008851 237669620 
713008911 237669640 
713009211 237669740 
713009481 237669830 
713009991 237670000 
713010021 237670010 
713010171 237670060 
713010531 237670180 
713010561 237670190 
713011281 237670430 
713011641 237670550 
713011731 237670580 
713011911 237670640 
713012151 237670720 
713012361 237670790 
713012451 237670820 
713012511 237670840 
713012721 237670910 
713012991 237671000 
713013141 237671050 
713013321 237671110 
713013771 237671260 
713013801 237671270 
713013891 237671300 
713014041 237671350 
713014131 237671380 
713014431 237671480 
713014611 237671540 
713015421 237671810 
713015481 237671830 
713015691 237671900 
713015781 237671930 
713016201 237672070 
713016321 237672110 
713016651 237672220 
713016831 237672280 
713016921 237672310 
713017131 237672380 
713017671 237672560 
713017761 237672590 
713017911 237672640 
713018091 237672700 
713018181 237672730 
713018211 237672740 
713018391 237672800 
713018571 237672860 
713019171 237673060 
713019261 237673090 
713019381 237673130 
713019621 237673210 
713019891 237673300 
 

Sunday, July 4, 2021

Creating Python Wrapper for one C++ procedure been inspired by Yandex Q news wire in Mathematics Version 2

 (.env) [boris@fedora33server YANDEXDBA]$ cat test.h

#pragma once

#include <cstddef>

#include <vector>

namespace abc {

std::vector<int>  catchList(int q,int m,int pw1,int pw2) ;

}  

(.env) [boris@fedora33server YANDEXDBA]$ cat test.cpp

#include <Python.h>

#include <iostream>

#include <vector>

#include <cmath>

#include "test.h"

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

int digitSum(int n)  

{  

  int sum=0,m;    

  while(n>0)    

  {    

    m=n%10;    

    sum=sum+m;    

    n=n/10;    

  }    

return sum;  

}  

std::vector<int>  catchList(int k,int m,int pw1,int pw2) {

 std::vector<int> result;

 int j,z;

 for( j=k; j < m; ++j)

  {

    z = digitSum(j);

    if (pow(double(z),pw1) == pow(double(j),pw2))

              result.push_back(j);

  }

 return result;

 }

extern "C"{}

namespace {

static PyObject *caughtList(PyObject* self, PyObject* args)

{

   int p,n;

   int pwr1,pwr2;

  // Now four arguments pass through PyArg_ParseTuple

   if(!PyArg_ParseTuple(args,"iiii" ,&p,&n,&pwr1,&pwr2))

       return NULL;

   std::vector<int>numbers = abc::catchList(p,n,pwr1,pwr2);

   PyObject* result = PyList_New(numbers.size());

   for(int i = 0; i < numbers.size(); i++) {

     PyList_SetItem(result, i, PyLong_FromLong(numbers[i]));

   }

   return result;  

};

// Our Module's Function Definition struct

// We require this `NULL` to signal the end of our method

// definition

static PyMethodDef myMethods[] = {

   { "caughtList", caughtList, METH_VARARGS, "Calculate prime numbers" },

   { NULL, NULL, 0, NULL }

};

// Our Module Definition struct

static struct PyModuleDef myModule = {

   PyModuleDef_HEAD_INIT,

   "myModule",

   "Test Module",

   -1,

   myMethods

};

// Initializes our module using our above struct

PyMODINIT_FUNC PyInit_myModule(void)

{

   return PyModule_Create(&myModule);

};

} // namespace(.env) 

[boris@fedora33server YANDEXDBA]$ cat setup.py

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['test.cpp'])])

(.env) [boris@fedora33server YANDEXDBA]$ python setup.py install

running install

running build

running build_ext

building 'myModule' extension

creating build

creating build/temp.linux-x86_64-3.9

gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/YANDEXDBA/.env/include -I/usr/include/python3.9 -c test.cpp -o build/temp.linux-x86_64-3.9/test.o

creating build/lib.linux-x86_64-3.9

g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/test.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so

running install_lib

copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages

running install_egg_info

Removing /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info

Writing /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info

(.env) [boris@fedora33server YANDEXDBA]$ cat MyProg1.py
import myModule
minV =  int(input("Input scaning start value :" ))
maxV =  int(input("Input scaning finish value :" ))
p1 = int(input("Input power for digitSum :" ))
p2 = int(input("Input power for Number to detect: " ))
print(myModule.caughtList(minV,maxV,p1,p2))






























Friday, July 2, 2021

Creating Python Wrapper for one C++ procedure been inspired by Yandex Q news wire in Mathematics

UPDATE as of 03/07/2021 15:36 MSK

To obtain the answer for particular question consider version of procedure catchList( ) with loop from 10 to 100 instead of 100 to 1000.

std::vector<int>  catchList(int m,int pw1,int pw2) {

 std::vector<int> result;

 int j,z;

 for( j=10; j < m; ++j)

  {

    z = digitSum(j);

    if (pow(double(z),pw1) == pow(double(j),pw2))

              result.push_back(j);

  }

 return result;

 }

(.env) [boris@fedora33server YANDEXQ]$ python setup.py install

Next step is addressing exactly question has been asked

(.env) [boris@fedora33server YANDEXQ]$ cat MyProg1.py

import myModule

p1 = int(input("input power for digitSum :" ))

p2 = int(input("input power for Number to detect: " ))

print(myModule.caughtList(100,p1,p2))

(.env) [boris@fedora33server YANDEXQ]$ python MyProg1.py

input power for digitSum :3

input power for Number to detect: 2

[27]














END UPDATE

(.env) [boris@fedora33server YANDEXQ]$ cat test.h

#pragma once

#include <cstddef>

#include <vector>

namespace abc {

std::vector<int>  catchList(int m,int pw1,int pw2) ;

}  

(.env) [boris@fedora33server YANDEXQ]$ cat test.cpp

#include <Python.h>

#include <iostream>

#include <vector>

#include <cmath>

#include "test.h"

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

int digitSum(int n)  

{  

  int sum=0,m;    

  while(n>0)    

  {    

    m=n%10;    

    sum=sum+m;    

    n=n/10;    

  }    

return sum;  

}  

std::vector<int>  catchList(int m,int pw1,int pw2) {

 std::vector<int> result;

 int j,z;

 for( j=100; j < m; ++j)

  {

    z = digitSum(j);

    if (pow(double(z),pw1) == pow(double(j),pw2))

              result.push_back(j);

  }

 return result;

 }

extern "C"{}

namespace {

// myModule.caughtList(1000,power1,power2)

// is supposed to be invoked from Python script

static PyObject *caughtList(PyObject* self, PyObject* args)

{

   int n;

   int pwr1,pwr2;

   if(!PyArg_ParseTuple(args,"iii",&n,&pwr1,&pwr2))

       return NULL;

   std::vector<int>numbers = abc::catchList(n,pwr1,pwr2);

   PyObject* result = PyList_New(numbers.size());

   for(int i = 0; i < numbers.size(); i++) {

     PyList_SetItem(result, i, PyLong_FromLong(numbers[i]));

   }

   return result;  

};

// Our Module's Function Definition struct

// We require this `NULL` to signal the end of our method

// definition

static PyMethodDef myMethods[] = {

   { "caughtList", caughtList, METH_VARARGS, "Calculate prime numbers" },

   { NULL, NULL, 0, NULL }

};

// Our Module Definition struct

static struct PyModuleDef myModule = {

   PyModuleDef_HEAD_INIT,

   "myModule",

   "Test Module",

   -1,

   myMethods

};

// Initializes our module using our above struct

PyMODINIT_FUNC PyInit_myModule(void)

{

   return PyModule_Create(&myModule);

};

} // namespace

(.env) [boris@fedora33server YANDEXQ]$ cat setup.py
from distutils.core import setup, Extension
import sysconfig

language = 'c++'
std = 'c++20'
# GCC flags to compile C++ Code
default_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]
setup(name = 'myModule', version = '1.0',  \
   ext_modules = [Extension('myModule', ['test.cpp'])])

Now we are going to build shared library which would contain required procedure. Notice we are in Python Virtual Environment  to avoid problems with "install".

(.env) [boris@fedora33server YANDEXQ]$ python setup.py install
running install
running build
running build_ext
building 'myModule' extension
creating build
creating build/temp.linux-x86_64-3.9
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/YANDEXQ/.env/include -I/usr/include/python3.9 -c test.cpp -o build/temp.linux-x86_64-3.9/test.o
creating build/lib.linux-x86_64-3.9
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/test.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
Writing /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info

******************
Make sure we did it 
******************

(.env) [boris@fedora33server YANDEXQ]$ cd build
(.env) [boris@fedora33server build]$ pwd
/home/boris/YANDEXQ/build
(.env) [boris@fedora33server build]$ ls -CRl
.:
total 0
drwxrwxr-x. 2 boris boris 53 Jul  2 20:42 lib.linux-x86_64-3.9
drwxrwxr-x. 2 boris boris 20 Jul  2 20:42 temp.linux-x86_64-3.9

./lib.linux-x86_64-3.9:
total 84
-rwxrwxr-x. 1 boris boris 82696 Jul  2 20:42 myModule.cpython-39-x86_64-linux-gnu.so

./temp.linux-x86_64-3.9:
total 128
-rw-rw-r--. 1 boris boris 127024 Jul  2 20:42 test.o

(.env) [boris@fedora33server YANDEXQ]$ cat MyProg1.py
import myModule
p1 = int(input("input power for digitSum :" ))
p2 = int(input("input power for Number to detect: " ))
print(myModule.caughtList(1000,p1,p2))