PDA

View Full Version : Vampire Number


tana
26 มิถุนายน 2004, 10:19
ใครรู้จัก Vampire Number บ้างครับ เลข 2 จำนวนที่คูณกันแล้วได้ออกมาเป็น 2 จำนวนนั้นแต่ตำแหน่งไม่แน่นอน เช่น

1260=21 · 60 , 1395=15 · 93 , 1435=35 · 41 , 1530=30 · 51

อยากรู้ว่าเราสามารถเขียนออกมาเป็นโปรแกรมให้คอมช่วยคำนวณได้ป่าวอ่ะคับ แล้วใครรู้ที่มาของมันบ้างก็ช่วยบอกหน่อยนะครับ ขอบคุณครับ

TOP
26 มิถุนายน 2004, 12:30
ก็เพิ่งรู้จักจากน้อง tana นี่ละครับ ดูจากนิยามของมันแล้ว เขียนโปรแกรมได้ง่ายมากครับ แค่แยกตัวประกอบ แล้วก็ตรวจสอบตามนิยามของมัน ลองดูจากที่นี่เพิ่มเติมครับ Vampire Number (http://mathworld.wolfram.com/VampireNumber.html) เห็นว่ามีคนค้นพบสูตร สำหรับสร้าง Vampire Number บางรูปแบบ ส่วนที่มาพี่ก็ไม่ทราบครับ ไม่ได้ค้นต่อ แต่คงไม่ใช่ Vampire ที่ไหนหรอก :D

tana
27 มิถุนายน 2004, 18:02
แล้วถ้าพี่เขียนโปรแกรมพี่ชอบใช้ภาษาอะไรเขียนเหรอครับ ที่ผมชอบใช้ก็เป็น Turbo C อ่ะครับ แล้วจะกำหนดตำแหน่งของเลขยังไงดีหละครับ เพื่อจะให้มันตรวจสอบได้น่ะครับ เพราะตำแหน่งสลับกันมั่วเลยด้วย แล้วก็ต้องเทียบเป็นตัวๆ ด้วยอ่ะครับ

TOP
28 มิถุนายน 2004, 17:29
พี่ใช้ภาษา C ครับ ตำแหน่งตัวเลขไม่ต้องสนใจสิครับ แค่นับจำนวนของ ตัวเลขแต่ละตัวที่มี ให้ตรงกันก็พอ

ลองเอา Code นี้ไปแกะดูครับ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define TRUE 1
#define FALSE 0

int isMatchDigit(char *szFirstNumber, char *szSecondNumber);
int isVampireNumber(long lNumber, long *plFirstFactor, long *plSecondFactor);

void main(int argc, char *argv[])
{
long lMaxNumber;
long lNumber;
long lFirstFactor, lSecondFactor;

if(argc != 2)
{
fprintf(stderr, "Usage : findvampire.exe upperbound_number\n");
return;
}
else
lMaxNumber = atol(argv[1]);

for(lNumber = 2; lNumber <= lMaxNumber; lNumber++)
if(isVampireNumber(lNumber, &lFirstFactor, &lSecondFactor) == TRUE)
fprintf(stdout, "Found Vampire Number %ld = %ld * %ld\n", lNumber, lFirstFactor, lSecondFactor);
}

int isMatchDigit(char *szFirstNumber, char *szSecondNumber)
{
int arDigitCount[10];
int iIndex;
int iNumDigit = strlen(szFirstNumber);

memset(arDigitCount, 0x00, sizeof(arDigitCount));

for(iIndex = iNumDigit-1; iIndex >= 0; iIndex--)
arDigitCount[*(szFirstNumber + iIndex) - '0']++;

for(iIndex = strlen(szSecondNumber) - 1; iIndex >= 0; iIndex--)
{
if(arDigitCount[*(szSecondNumber + iIndex) - '0'] > 0)
{
arDigitCount[*(szSecondNumber + iIndex) - '0']--;
iNumDigit--;
}
else
return FALSE;
}

if(iNumDigit > 0)
return FALSE;
else
return TRUE;
}

int isVampireNumber(long lNumber, long *plFirstFactor, long *plSecondFactor)
{
char szFirstNumber[20+1];
char szSecondNumber[20+1];

long lUpperBound = floor(sqrt(lNumber));
long lDivideNumber;

for(lDivideNumber = 2; lDivideNumber <= lUpperBound; lDivideNumber++)
{
if((lNumber%lDivideNumber) == 0)
{
*plFirstFactor = lDivideNumber;
*plSecondFactor = lNumber / (*plFirstFactor);

sprintf(szFirstNumber, "%ld", lNumber);
sprintf(szSecondNumber, "%ld%ld", *plFirstFactor, *plSecondFactor);
if(isMatchDigit(szFirstNumber, szSecondNumber) == TRUE)
return TRUE;
}
}
return FALSE;
}

ตัวอย่างการใช้ และผลลัพธ์ที่ได้

findvampire.exe 12345

Found Vampire Number 126 = 6 * 21
Found Vampire Number 153 = 3 * 51
Found Vampire Number 688 = 8 * 86
Found Vampire Number 1206 = 6 * 201
Found Vampire Number 1255 = 5 * 251
Found Vampire Number 1260 = 6 * 210
Found Vampire Number 1395 = 15 * 93
Found Vampire Number 1435 = 35 * 41
Found Vampire Number 1503 = 3 * 501
Found Vampire Number 1530 = 3 * 510
Found Vampire Number 1827 = 21 * 87
Found Vampire Number 2187 = 27 * 81
Found Vampire Number 3159 = 9 * 351
Found Vampire Number 3784 = 8 * 473
Found Vampire Number 6880 = 8 * 860
Found Vampire Number 10251 = 51 * 201
Found Vampire Number 10255 = 5 * 2051
Found Vampire Number 10426 = 26 * 401
Found Vampire Number 10521 = 21 * 501
Found Vampire Number 10525 = 5 * 2105
Found Vampire Number 10575 = 15 * 705
Found Vampire Number 11259 = 9 * 1251
Found Vampire Number 11844 = 84 * 141
Found Vampire Number 11848 = 8 * 1481
Found Vampire Number 12006 = 6 * 2001
Found Vampire Number 12060 = 6 * 2010