[ESERCIZIO] - Calcolatore

« Older   Newer »
  Share  
FrontBack
CAT_IMG Posted on 2/4/2011, 17:32     +1   -1




In un concorso di matematica ho trovato questo problema da risolvere:
CITAZIONE
Trova il primo numero che sia con un numero di cifre pari, palindromo e primo.

Questo è ciò che ho elaborato in 15 minuti.

SPOILER (click to view)
CODICE
#include <_Primes.au3>
#include <String.au3>
#include <Math.au3>

Global $iMin = 12
Global $iMax = 999999999
Global $count = $iMin

Do
       ConsoleWrite("Numero analizzato: "&$count&"                ")
       $spl = StringSplit($count, "")
       If _MathCheckDiv(Number($spl[0]), 2) = 2 Then
               $pare = 1
       Else
               $pare = 0
       EndIf
       $spl = StringSplit($count, "")
       If $count = Number(_StringReverse($count)) And _IsPrime($count) = 1 And $pare = 1 Then
               ConsoleWrite("Sì"&@CRLF)
               MsgBox(0, "Calcolatore", "Il primo numero primo palindromo pari è: "&$count)
               ExitLoop
       Else
               ConsoleWrite("No"&@CRLF)
               $count += 1
               ContinueLoop
       EndIf
Until $count = $iMax
ConsoleWrite("Completato"&@CRLF)


Edited by xMasteRx - 2/4/2011, 18:46
 
Top
I.Ren
CAT_IMG Posted on 2/4/2011, 18:04     +1   -1




Dopo vedo di farlo in c : D

Edit: fatto.

SPOILER (click to view)
CODICE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {true,false} bool;

bool n_pari(int num);
bool n_primo(int num);
bool n_palindromo(int num);

int main()
{
   int n=0;
   while (1==1)
   {
       if (n_pari(n)==true && n_primo(n) == true && n_palindromo(n) == true)
       {
           break;
       }
       n++;
   }
   printf("Il primo numero trovato e': %d",n);
   return 0;
}

bool n_pari(int num)
{
   if (num%2 == 0)
       return true;
   else
       return false;
}

bool n_primo(int num)
{
   int pos = (num+1)/2;
   while (pos > 1)
   {
       if (num % pos == 0 && pos != num && pos != 1) return false;
       pos--;
   }
   return true;
}

bool n_palindromo(int num)
{
   char to_str[4096];
   char *buff = NULL;
   itoa(num,to_str,10);
   buff = realloc(buff,strlen(to_str)+1*sizeof(char));
   strcpy(buff,to_str);
   strrev(buff);
   if (strcmp(to_str,buff) == 0)
       return true;
   else
       return false;
   free(buff);
}

master: non vi piaciono i spoiler vero xD

Edited by xMasteRx - 2/4/2011, 20:05
 
Top
MHack
CAT_IMG Posted on 3/4/2011, 19:17     +1   -1




Per favore mettete il nome giusto al titolo.
 
Top
2 replies since 2/4/2011, 17:32   132 views
  Share